C# 无法从sql数据库中读取int值
我在C中尝试过这段代码,但它不起作用-我无法获得输入id,每次运行它时,id的值都是0C# 无法从sql数据库中读取int值,c#,sql-server-2012,C#,Sql Server 2012,我在C中尝试过这段代码,但它不起作用-我无法获得输入id,每次运行它时,id的值都是0 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=sms;Persist Security Info=True;User ID=boy;Password=coco"); int id; con.Open(); string sql = "select * from Staff_Management wh
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=sms;Persist Security Info=True;User ID=boy;Password=coco");
int id;
con.Open();
string sql = "select * from Staff_Management where Emp_Name = '"+sName+"'; ";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
id = read.GetInt32(0);
TM_AC_SelectId.Text = id.ToString();
}
else
{
MessageBox.Show("Error 009 ");
}
con.Close();
您应该尝试遵循公认的ADO.NET编程最佳实践: 为查询使用参数-始终-无例外 使用。。。{….}构造以确保正确、快速地处置您的资源 只选择你真正需要的列-不要只是因为懒散而使用select*-指定你真正需要的列! 将代码更改为:
// define connection string (typically loaded from config) and query as strings
string connString = "Data Source=.;Initial Catalog=sms;Persist Security Info=True;User ID=boy;Password=coco";
string query = "SELECT id FROM dbo.Staff_Management WHERE Emp_Name = @EmpName;";
// define SQL connection and command in "using" blocks
using (SqlConnection con = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(query, con))
{
// set the parameter value
cmd.Parameter.Add("@EmpName", SqlDbType.VarChar, 100).Value = sName;
// open connection, execute scalar, close connection
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
int id;
if(result != null)
{
if (int.TryParse(result.ToString(), out id)
{
// do whatever when the "id" is properly found
}
}
}
您应该尝试遵循公认的ADO.NET编程最佳实践: 为查询使用参数-始终-无例外 使用。。。{….}构造以确保正确、快速地处置您的资源 只选择你真正需要的列-不要只是因为懒散而使用select*-指定你真正需要的列! 将代码更改为:
// define connection string (typically loaded from config) and query as strings
string connString = "Data Source=.;Initial Catalog=sms;Persist Security Info=True;User ID=boy;Password=coco";
string query = "SELECT id FROM dbo.Staff_Management WHERE Emp_Name = @EmpName;";
// define SQL connection and command in "using" blocks
using (SqlConnection con = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(query, con))
{
// set the parameter value
cmd.Parameter.Add("@EmpName", SqlDbType.VarChar, 100).Value = sName;
// open connection, execute scalar, close connection
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
int id;
if(result != null)
{
if (int.TryParse(result.ToString(), out id)
{
// do whatever when the "id" is properly found
}
}
}
它不起作用,我们经常收到这样的消息——事实上,太多了。并修复不起作用的部分-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-检查您是否200%确定SELECT*中的第一列确实是您的id??如果您真的只需要Id,那么为什么要使用SELECT*呢?请使用SELECT Id FROM。。。。然后您可以使用.ExecuteScalar函数来获取您感兴趣的一个值!它不起作用,我们经常收到这样的消息——事实上,太多了。并修复不起作用的部分-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-检查您是否200%确定SELECT*中的第一列确实是您的id??如果您真的只需要Id,那么为什么要使用SELECT*呢?请使用SELECT Id FROM。。。。然后您可以使用.ExecuteScalar函数来获取您感兴趣的一个值!OP,显式指定列的原因之一,也是为了帮助将任何异常的关系保持在源代码中。例如,在您的方法中,如果假设第一列是int,但它是字符串,那么异常或错误会从读取器中冒出来,而事实上问题是在查询中。如果您是显式的,那么错误可能会告诉您您的查询不好,而不是您对resultset的使用不好。最后,OP的问题假设只会有一个或零个结果,这是一个脆弱的假设,尤其是当使用名称作为过滤器John/Jennifer Smith?时?。最好还是坚持使用ExecuteCommand+reader,以更明确的方式处理多个resultset,使其具有控制权,而不是将其留给chance.OP。明确指定列的原因之一,也是为了帮助将任何异常的关系植根于源代码中。例如,在您的方法中,如果假设第一列是int,但它是字符串,那么异常或错误会从读取器中冒出来,而事实上问题是在查询中。如果您是显式的,那么错误可能会告诉您您的查询不好,而不是您对resultset的使用不好。最后,OP的问题假设只会有一个或零个结果,这是一个脆弱的假设,尤其是当使用名称作为过滤器John/Jennifer Smith?时?。最好还是坚持使用ExecuteCommand+reader,以一种更明确的方式处理多个结果集,这种方式可以让您进行控制,而不是把它留给偶然的机会。