我正在查看参数化查询问题我找不到使用SqlDataReader
和参数化查询来填充下拉列表的示例。
现在我可以使用我的代码
来填充我的下拉if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(SQLConnectionString))
{
System.Data.SqlClient.SqlCommand go = new System.Data.SqlClient.SqlCommand();
con.Open();
go.Connection = con;
go.CommandText = "SELECT InsuredID, FirstName, LastName FROM [Lab2].[dbo].[INSURED]";
go.ExecuteNonQuery();
SqlDataReader readIn = go.ExecuteReader();
while (readIn.Read())
{
ddlHomeInsuredID.Items.Add(
new ListItem(readIn["InsuredID"].ToString() + " : " + readIn["FirstName"].ToString()
+ " " + readIn["LastName"].ToString()));
}
con.Close();
ddlHomeInsuredID.Items.Insert(0, new ListItem("--Select InsuredID--", "0"));
}
}
但是,我想让这个select语句参数化。我怎样才能做到这一点? 我很乐意编写参数化的插入语句,如下所示:
using (SqlConnection connection = new SqlConnection(SQLConnectionString))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = @"INSERT INTO [Lab2].[dbo].[INSURED] ([FirstName], [LastName], [MI], [DateOfBirth],
[CreditScore], [AddressID], [DriversLicenseNumber], [LastUpdatedBy], [LastUpdated]) VALUES
(@firstName, @lastName, @middleInitial, @dateOfBirth, @creditScore, @addressID,
@driversLicenseNumber, @lastUpdatedBy, @lastUpdated)";
command.Parameters.Add("@firstName", SqlDbType.VarChar, 20).Value = Insured.insuredArr[j].getFirstName();
command.Parameters.Add("@lastName", SqlDbType.VarChar, 30).Value = Insured.insuredArr[j].getLastName();
command.Parameters.Add("@middleInitial", SqlDbType.Char, 1).Value = Insured.insuredArr[j].getMiddleInitial();
command.Parameters.Add("@dateOfBirth", SqlDbType.VarChar, 30).Value = Insured.insuredArr[j].getDateOfBirth();
command.Parameters.Add("@creditScore", SqlDbType.Int).Value = Insured.insuredArr[j].getCreditScore();
command.Parameters.Add("@addressID", SqlDbType.Int).Value = Insured.insuredArr[j].getAddressID();
command.Parameters.Add("@driversLicenseNumber", SqlDbType.VarChar, 30).Value = Insured.insuredArr[j].getDriversLicenseNumber();
command.Parameters.Add("@lastUpdatedBy", SqlDbType.VarChar, 20).Value = Insured.insuredArr[j].getLastUpdatedBy();
command.Parameters.Add("@lastUpdated", SqlDbType.Date).Value = Insured.insuredArr[j].getLastUpdated();
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
MsgBox("Record(s) inserted into database", this.Page, this);
那么,我怎样才能像第二个例子那样进行第一次查询?
由于
nammrick
答案 0 :(得分:6)
首先,ExecuteNonQuery()
方法的使用对SELECT
查询无效,只需坚持ExecuteReader()
,因为您要返回查询结果。这是ExecuteNonQuery
方法的用法说明:
您可以使用ExecuteNonQuery执行目录操作(for 例如,查询数据库的结构或创建数据库 诸如表之类的对象,或者在没有的情况下更改数据库中的数据 通过执行 UPDATE,INSERT或DELETE语句来使用DataSet。
修改后的查询流程应如下所示:
using (SqlConnection con = new SqlConnection(SQLConnectionString))
{
SqlCommand go = new SqlCommand();
con.Open();
go.Connection = con;
go.CommandText = "SELECT InsuredID, FirstName, LastName FROM [Lab2].[dbo].[INSURED]";
SqlDataReader readIn = go.ExecuteReader();
while (readIn.Read())
{
// reading data from reader
}
con.Close();
// other stuff
}
如果要对SELECT
语句使用参数化查询,则需要在WHERE
子句中包含至少一个列(和一个参数名称)(参见下面的示例):
SELECT InsuredID, FirstName, LastName FROM [Lab2].[dbo].[INSURED] WHERE InsuredID = @InsuredID
然后,您可以使用SqlParameter
将参数值传递到上面的查询中:
using (SqlConnection con = new SqlConnection(SQLConnectionString))
{
System.Data.SqlClient.SqlCommand go = new System.Data.SqlClient.SqlCommand();
con.Open();
go.Connection = con;
go.CommandText = "SELECT InsuredID, FirstName, LastName FROM [Lab2].[dbo].[INSURED] WHERE InsuredID = @InsuredID";
go.Parameters.Add("@InsuredID", SqlDbType.Int).Value = 1; // example value for parameter passing
SqlDataReader readIn = go.ExecuteReader();
while (readIn.Read())
{
// reading data from reader
}
con.Close();
// other stuff
}
注意:避免在INSERT/UPDATE/DELETE
语句使用相同活动连接填充数据的同时执行SELECT
操作,应先执行另一个查询之前先关闭先前的连接。
更多例子: