参数异常在sql连接中未处理

时间:2016-05-04 06:44:51

标签: c# sql wpf

当我尝试运行我的程序时出现错误 -

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Initialiseringsstrengens format does not match the specification starting at index 0.

using (conn = new SqlConnection(connstring))

整个代码 -

public partial class MainWindow : Window
{


    public SqlConnection conn;
    public SqlCommand cmd;
    string connstring = (@"Data Source=SINDALSQL\MSSQL14; Initial Catalog=OminiData; Integrated Security =True");
    string sql = ("SELECT Mærke, Model, Årgang, [Motor Type], Krydsmål, Centerhul, Møtrik, Bolter, Dæk, Fælge FROM Hjuldata");

    private void binddata()
    {
        DataSet1 ds = new DataSet1();
        using (conn = new SqlConnection(connstring))
        {
            cmd = new SqlCommand(sql, conn);
            SqlDataAdapter adp = new SqlDataAdapter();
            conn.Open();
            adp.SelectCommand = cmd;
            adp.Fill(ds, "Hjuldata");
            hjuldata.DataContext = ds;
        }
    }`

1 个答案:

答案 0 :(得分:0)

public partial class MainWindow : Window { 
    public SqlConnection conn;
    public SqlCommand cmd;
    string connstring  = (@"Data Source=SINDALSQL\MSSQL14; Initial Catalog=OminiData; Integrated Security =True");
    string  sql= ("SELECT Mærke, Model, Årgang, [Motor Type], Krydsmål, Centerhul, Møtrik, Bolter, Dæk, Fælge FROM Hjuldata");

    private void binddata() {
        DataTable dt = new DataTable();
        using (SqlDataAdapter adp = new SqlDataAdapter(sql,connstring)) {
            adp.Fill(dt);
            hjuldata.DataContext = dt;
        }
    }
}