2011-10-27 21 views
0

我是C#的新手。未將對象引用設置爲對象的實例。請幫助解決錯誤

請告訴我這段代碼有什麼問題。我使用兩個輸入字段EndValueTextBox和StartValueTextBox將數據插入到數據庫中。

我收到以下錯誤。 「對象引用不設置到對象的實例」

private void buttonSave_Click(object sender, EventArgs e) 
{ 
    connection = new System.Data.SqlClient.SqlConnection(); 
    da = new SqlDataAdapter(); 
    try 
    { 
     connection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='G:\\C#.Net\\Forms Practice\\WindowsFormsPractice1\\WindowsFormsPractice1\\WindowsFormsPractice1.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"; 
    } 
    catch (System.Exception ex) 
    { 
     MessageBox.Show(ex.Message,"Connection String"); 
    } 
    try 
    { 
     connection.Open(); 
     string sql = "insert into TBLWORKERS (first_name , last_name)" + " values('" + StartValueTextBox.Text + "', '" + EndValueTextBox.Text + ")"; 
     //SqlDataAdapter da = new SqlDataAdapter(query, connString); 


     da.InsertCommand.CommandText = sql; 

     da.InsertCommand.ExecuteNonQuery(); 

    } 
    catch (System.Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Connection open"); 
    } 
} 
+0

關於您創建SQL的方式的警告。您有Sql注入的安全風險。這意味着如果有人會在其中一個texbox中輸入一些sql,而不是正常的開始/結束值,那麼sql將針對數據庫執行(例如,「drop table」命令!) –

+0

@Wouter:OP沒有明白你的意思。如何提供一個鏈接到SQL注入文章,描述如何參數化? –

+2

@RobertHarvey你是對的:)這個鏈接:http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx有一個很好的介紹如何構建一個SQL查詢 –

回答

2

SqlDataAdapter從未分配到執行的查詢的連接。您需要在施工期間或施工後將SqlConnectionSqlDataAdapter聯繫起來。

+1

如果OP對C#來說真的是新手,那麼對他來說這是希臘語。代碼示例如何? –

0
string connetionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename='G:\\C#.Net\\Forms Practice\\WindowsFormsPractice1\\WindowsFormsPractice1\\WindowsFormsPractice1.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True"; 

SqlDataAdapter adapter = new SqlDataAdapter(); 

string sql = "insert into TBLWORKERS (first_name , last_name)" + " values('" + StartValueTextBox.Text + "', '" + EndValueTextBox.Text + ")"; 

SqlConnection connection = new SqlConnection(connetionString); 
try { 
    connection.Open(); 
    adapter.InsertCommand = new SqlCommand(sql, connection); 
    adapter.InsertCommand.ExecuteNonQuery(); 
} catch (Exception ex) { 
    MessageBox.Show(ex.Message); 
} 
1

此行da.InsertCommand.CommandText = sql;必須以這種方式:

da.InsertCommand = new SqlCommand(sql); 
+0

對不起,不得不糾正我的樣本 – Fischermaen

+0

你也可以用OP的方式來使用字符串。 –

0

在你是什麼時候的異常?也許那些線

System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(); 
SqlDataAdapter da = new SqlDataAdapter(); 
+1

這兩條線都不是例外 - 它們很簡單。 – SliverNinja

0

這裏是你的代碼(未測試)應該照顧SqlDataAdapter沒有分配的連接對象的未成年重寫,並演示瞭如何使用參數化查詢,以幫助抵禦SQL注入攻擊:

private void buttonSave_Click(object sender, EventArgs e) 
{ 

    try 
    { 
     // The using block will automatically dispose of your connection when 
     // the block is exited and is considered standard practice. 
     using (SqlConnection connection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='G:\\C#.Net\\Forms Practice\\WindowsFormsPractice1\\WindowsFormsPractice1\\WindowsFormsPractice1.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";)) 
     { 

      SqlDataAdpter da = new SqlDataAdapter(); 

      connection.Open(); 

      // Assign the SqlConnection object to the SqlDataAdapter 
      da.Connection = connection; 

      // Parameterize the query as shown below 
      string sql = "INSERT INTO TBLWORKERS(first_name, last_name) VALUES(@first_name, @last_name)"; 

      da.InsertCommand.CommandText = sql; 

      // Add the values for the parameters 
      da.InsertCommand.Parameters.Add("@first_name", SqlDbType.NVarChar, 25, StartValueTextBox.Text); 
      da.InsertCommand.Parameters.Add("@last_name", SqlDbType.NVarChar, 25, EndValueTextBox.Text); 

      // Execute the query - rows will have the number of rows 
      // affected. should be 1 in this case if succesful 
      int rows = da.InsertCommand.ExecuteNonQuery();   
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Connection open"); 
    } 
} 
相關問題