C# 对象引用未设置为对象的实例。请帮助解决错误
我是C#的新手 请告诉我这个代码有什么问题。我使用两个输入字段EndValueTextBox和StartValueTextBox在数据库中插入数据 我收到以下错误。“对象引用未设置为对象的实例”C# 对象引用未设置为对象的实例。请帮助解决错误,c#,winforms,C#,Winforms,我是C#的新手 请告诉我这个代码有什么问题。我使用两个输入字段EndValueTextBox和StartValueTextBox在数据库中插入数据 我收到以下错误。“对象引用未设置为对象的实例” 您的SqlDataAdapter从未被分配执行查询的连接。在构造期间或之后,您需要将SqlConnection与SqlDataAdapter关联。您的SqlDataAdapter从未分配要执行查询的连接。您需要在构造期间或之后将SqlConnection与SqlDataAdapter关联。此行da.In
您的
SqlDataAdapter
从未被分配执行查询的连接。在构造期间或之后,您需要将SqlConnection
与SqlDataAdapter
关联。您的SqlDataAdapter
从未分配要执行查询的连接。您需要在构造期间或之后将SqlConnection
与SqlDataAdapter
关联。此行da.InsertCommand.CommandText=sql代码>必须以这种方式:
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);
}
da.InsertCommand = new SqlCommand(sql);
此行da.InsertCommand.CommandText=sql代码>必须以这种方式:
da.InsertCommand = new SqlCommand(sql);
什么时候你是例外?可能是那条线
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
什么时候你是例外?可能是那条线
System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
SqlDataAdapter da = new SqlDataAdapter();
下面是对代码(未经测试)的一次小规模重写,它应该能够解决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");
}
}
下面是对代码(未经测试)的一次小规模重写,它应该能够解决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");
}
}
关于创建SQL的方式的警告。您正面临Sql注入的安全风险。这意味着,如果有人将在其中一个文本框中输入一些sql,而不是一个正常的开始/结束值,那么将对数据库执行sql(例如“drop table”命令!)@Wouter:OP不理解您的意思。提供一个SQL注入文章的链接,描述如何参数化,怎么样?@RobertHarvey你是对的:)这个链接:对如何构造SQL查询有一个很好的介绍。一个try/catch语句也就足够了-你不需要多个。关于创建SQL的方式的警告。您正面临Sql注入的安全风险。这意味着,如果有人将在其中一个文本框中输入一些sql,而不是一个正常的开始/结束值,那么将对数据库执行sql(例如“drop table”命令!)@Wouter:OP不理解您的意思。提供一个链接到描述如何参数化的SQL注入文章怎么样?@RobertHarvey你是对的:)这个链接:对如何构造SQL查询有一个很好的介绍。一个try/catch语句也就足够了-你不需要多个。如果OP对C#来说真的是个新手,这对他来说是个希腊语。一个代码示例如何?如果OP对C#来说真的是新的,那么这对他来说就是希腊语。代码示例如何?你也可以用OP的方式,使用字符串。你也可以用OP的方式,使用字符串。这两行都不能是例外-它们都是简单的系数。这两行都不能是例外-它们都是简单的系数。