System.Data.SqlClient.SqlException(0x80131904):“。”附近的语法不正确

Posted

技术标签:

【中文标题】System.Data.SqlClient.SqlException(0x80131904):“。”附近的语法不正确【英文标题】:System.Data.SqlClient.SqlException(0x80131904) : Incorrect syntax near '.' 【发布时间】:2018-06-28 09:39:58 【问题描述】:

我正在尝试填充更多信息以使银行对帐单具有更多信息,因此我决定加入两个表。此查询在 SQL 管理工作室上运行良好。但如果我在 Visual Studio 中使用来创建查询和显示数据,它会发送此异常错误

System.Data.SqlClient.SqlException(0x80131904):附近的语法不正确 '.'。

在第 62 行

我的代码如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace TmpZ

    public partial class BalanceSheet : Form
    
        string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
        public BalanceSheet()
        
            InitializeComponent();
        

        private void BalanceSheet_Load(object sender, EventArgs e)
        

        

        private void reportViewer1_Load(object sender, EventArgs e)
        

        

        private void button1_Click(object sender, EventArgs e)
        
            if (accountNo1.Text == "")
            
                MessageBox.Show("Please Enter Account Number");
            
            else
            
                DataTable dtb = new DataTable();
                dtb = GenerateBankStatement(dtb);
                reportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource rpd = new ReportDataSource("DataSet1", dtb);
                reportViewer1.LocalReport.DataSources.Add(rpd);
                reportViewer1.RefreshReport();
            
        

        private DataTable GenerateBankStatement(DataTable dt)
        
            using (SqlConnection cn = new SqlConnection(constring))
            
                try
                
                    string dateF = Convert.ToDateTime(dateFrom.Text).ToString("dd-MM-yyyy");
                    string dateT = Convert.ToDateTime(dateTo.Text).ToString("dd-MM-yyyy");
                    //SqlDataAdapter da = new SqlDataAdapter("SELECT [id] as id, [transaction_desc] as transaction_desc,[credit] as credit, [debit] as debit, [balance] as balance, [transaction_date] as transaction_date FROM transactions WHERE(accountNo1 = '" + accountNo1.Text + "') AND(transaction_date BETWEEN '" + dateF + "' AND '" + dateT + "')", cn);
                    //SqlDataAdapter da = new SqlDataAdapter("SELECT [id] as id, [transaction_desc] as transaction_desc,[credit] as credit, [debit] as debit, [balance] as balance, [transaction_date] as transaction_date FROM transactions WHERE(accountNo1 = '" + accountNo1.Text + "')", cn);
                    SqlDataAdapter da = new SqlDataAdapter("SELECT [fullname] as account_info.fullname, [accountNo] as account_info.accountNo, [ccy] as account_info.ccy, [address] as account_info.address, [id] as transactions.id, [transaction_desc] as transactions.transaction_desc, [credit] as transactions.credit, [debit] as transactions.debit, [balance] as transactions.balance, [transaction_date] as transactions.transaction_date FROM  transactions CROSS JOIN account_info WHERE(account_info.accountNo = '" + accountNo1.Text + "') AND(transactions.transaction_date BETWEEN '" + dateF + "' AND '" + dateT + "')", cn);
                    da.Fill(dt);
                
                catch(Exception ex)
                
                    MessageBox.Show(ex.ToString());
                
            
            return dt;
        
    

第 62 行显示了 SQLDataAdapter。我做错了什么?

【问题讨论】:

你为什么把所有的都放在那里?你可以去"SELECT account_info.fullname, account_info.accountNo..."等等。我认为,如果您只输入"fullname",只要列名仅在一个表中,它甚至应该可以工作。至少我以前是这样做的,而且效果很好。 SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 谢谢,它的winform 【参考方案1】:

您不能在别名中使用.

SELECT [fullname] as account_info.fullname

我相信这就是你想要的

SELECT account_info.fullname as [fullname]

【讨论】:

这可行,但除了表格中的信息外,不显示其他信息。 @TimTim 其他信息是什么?

以上是关于System.Data.SqlClient.SqlException(0x80131904):“。”附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章