我试图填充更多信息,以使银行对帐单具有更多信息,因此我决定合并两个表。该查询在SQL Management Studio上运行良好。但是如果我在Visual Studio中使用它来创建查询和显示数据,它将发送此Exception错误


  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 [fullname] as account_info.fullname


我相信这就是你想要的

SELECT account_info.fullname as [fullname]

关于c# - System.Data.SqlClient.SqlException(0x80131904):'。附近的语法不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48336155/

10-08 22:37