【问题标题】:Data adapter fill dataset error ,SQL connection error数据适配器填充数据集错误,SQL连接错误
【发布时间】:2015-06-21 01:42:40
【问题描述】:

我正在尝试进行 SQL 查询并将结果保存在 XML 文件中,但我收到了我在此处附加的图片的错误 无效的列! 此查询在 SQL Server 中可以正常工作,但在这里不行

namespace xxxxxxxxxxxx
{
    public partial class MainWindow : Window
    {
        string vinValue;
        string operationValue;
        string serviceValue;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void FrameLoad_Activated(object sender, EventArgs e)
    {

    }

    private void btnGo_Click(object sender, RoutedEventArgs e)
    {

        DataSet ds = new DataSet();
        SqlDataAdapter adapter;

        string sql = String.Format("SELECT FGACJD_Vin_Ecu.Vin,FGACJD_Vin_Ecu.NHard,"+
            "FGACJD_Vin_Ecu.NVerHard,FGACJD_Vin_Ecu.NSoft,FGACJD_Vin_Ecu.NVerSoft,"+
            "InfoProg_wiTECH_Global.NHard,InfoProg_wiTECH_Global.NVerHard,"+
            "InfoProg_wiTECH_Global.NSoft,InfoProg_wiTECH_Global.NSoftNew,"+
            "InfoProg_wiTECH_Global.NVerSoft,InfoProg_wiTECH_Global.NomeFile,"+
            "InfoProg_wiTECH_Associa.KeyJoined,InfoProg_wiTECH_Associa.MakeID,"+
            "InfoProg_wiTECH_Associa.ModelID,InfoProg_wiTECH_Associa.Model_Type "+
            "FROM FGACJD_Vin_Ecu, InfoProg_wiTECH_Global,InfoProg_wiTECH_Associa "+
            "WHERE [FGACJD_Vin_Ecu.Vin]='{0}'"+
            " AND InfoProg_wiTECH_Associa.KeyJoined = InfoProg_wiTECH_Global.NomeFile"+
            " AND FGACJD_Vin_Ecu.NHard=InfoProg_wiTECH_Global.NHard"+
            " AND InfoProg_wiTECH_Global.NVerHard like '%'+FGACJD_Vin_Ecu.NVerHard "+
            "FOR XML PATH('flash'), ROOT ('FlashList ')", vinValue);


        //TextOutput.Text = sql;
        string connectionString = "user id=xxx;password=xxx;"+
                        "server=localhost;" +
                        "Trusted_Connection=yes;" +
                        "database=xxx; " +
                        "connection timeout=30";

        SqlConnection conn = new SqlConnection(connectionString);
        try
        {
            conn.Open();
        }
        catch (Exception)
        {
            string err = "Database error contact administrator";
            MessageBox.Show(err, "Error!");
        }

        try
        {

            adapter = new SqlDataAdapter(sql, conn);
            adapter.Fill(ds);
            conn.Close();
            ds.WriteXml("Product.xml");
            MessageBox.Show("Done");
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
   }

    private void TextInput_TextChanged(object sender, TextChangedEventArgs e)
    {
        vinValue = TextInput.Text;
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var comboBox = sender as ComboBox;
        serviceValue = comboBox.SelectedItem as string;
        this.Title = "Selected: " + serviceValue;
    }

    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> data = new List<string>();
        data.Add("getFlashListByVIN");
        var comboBox = sender as ComboBox;
        comboBox.ItemsSource = data;
        comboBox.SelectedIndex = 0;
    }

    private void Operation_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> data = new List<string>();
        data.Add("MOC");
        var comboBox = sender as ComboBox;
        comboBox.ItemsSource = data;
        comboBox.SelectedIndex = 0;
    }

    private void Operation_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var comboBox = sender as ComboBox;
        operationValue = comboBox.SelectedItem as string;
        this.Title = "Selected: " + operationValue;
    }
}

}

我收到此错误

System.Data.SqlClient.SqlException (0x80131904):列名无效 'FGACJD_Vin_Ecu.Vin'。在 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler、SqlDataReader 数据流、 BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & dataReady) 在 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 在 System.Data.SqlClient.SqlDataReader.get_MetaData() 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,字符串 resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior runBehavior、布尔 returnStream、布尔 异步,Int32 超时,任务和任务,布尔 asyncWrite,SqlDataReader ds) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法,TaskCompletionSource`1 完成,Int32 超时,Task& 任务, 布尔 asyncWrite) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior、runBehavior、布尔返回流、字符串 方法)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为)在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.FillInternal(DataSet 数据集,DataTable[] 数据表,Int32 startRecord,Int32 maxRecords, 字符串 srcTable、IDbCommand 命令、CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.Fill(数据集数据集,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand 命令, CommandBehavior 行为)在 System.Data.Common.DbDataAdapter.Fill(DataSet 数据集) 在 FlashListByVinLadan.MainWindow.btnGo_Click(对象发送者, RoutedEventArgs e) 在 c:\Users\Finsoft\Desktop\test\FlashListByVinLadan\FlashListByVinLadan\MainWindow.xaml.cs:line 80 ClientConnectionId:3653b64e-bb82-41cb-8fc8-4d92fbaa3c3e 错误 编号:207,状态:1,等级:16

【问题讨论】:

  • 无效的列名 'FGACJD_Vin_Ecu.Vin',您确定所有内容都与您的数据库匹配吗?
  • 使用String.Format 不会阻止您进行sql注入。而是使用 sql 参数。
  • 我也会使用verbatim string literal,然后您可以使用在 SSMS 中工作的相同查询。你只需要在前面加上@,然后你可以删除所有"+ ",即使是多行。
  • 使用参数,将[FGACJD_Vin_Ecu.Vin]改成[FGACJD_Vin_Ecu].[Vin]
  • sql 错了?参数?

标签: c# sql sql-server xml dataset


【解决方案1】:

sql 错误

WHERE FGACJD_Vin_Ecu.Vin='{0}'

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 2014-07-02
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多