在,net中怎么把从数据库中查询到的值转化成Int类型
string sqlstr = "select Placed from enterprise";
int x = Convert.ToInt32("sqlstr");
不明白怎么实现啊,Placed设置的是int类型,我想用x来做判断,应该怎么做啊
数据库 string .net
[解决办法]
类似这样写吧
int result = 0;
string connstr = "Data Source=(local);Initial Catalog=DatabaseName; User ID=UserName; Password=Pwd; ";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
conn.Open();
string sql = "select Placed from enterprise";//这里没个where条件么?比如where id=1之类的
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, conn);
object obj = cmd.ExecuteScalar();
if (obj != null)
{
result = int.TryParse(obj.ToString(),out result)==true?Convert.ToInt32(obj):result;
}