怎么把 sql语句查询的值赋给变量 string TypeID = "select ShowType from CustomerInfo where ID="+newsid; 这样写有错吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 错的!最好是先把查询的值填充到dataset里,在取出值! LZ先了解下sql是怎么执行吧。 public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } }供参考 你的写法是定义了一个string类型变量,这里是你要执行的操作,可以添加以下方式实现:SqlCommand cmd=new SqlCommand(TypeID,conn);string showType=cmd.ExecuteScalar().ToString(); 肯定错了啊~~先执行:SqlConnection connection = new SqlConnection(connectionString);SqlCommand cmd=new SqlCommand(TypeID,connection );再赋值: string TypeID =cmd.ExecuteScalar().ToString(); 错 语句都没执行就给值了public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } DataSet ds=new Query("select ShowType from CustomerInfo where ID="+newsid+") string TypeID =ds.table[0].rows[0][ShowType ].tostraing(); set var TypeID ="select ShowType from CustomerInfo where ID="+newsid;sql中赋值用 set 或则 while都可以 不好意思看错题目了string sql= "select ShowType from CustomerInfo where ID="+newsid这样是一个String类型用法如下:sqlconnection con=new sqlconnetion('连接数据库字符串');con.open()Sqlcommand cmd=new sqlcommand(sql,con);sqldataadapter adapter=new sqldataadapter(cmd);DataTable dt=new Datatable();adapter.fill(dt);con.close(); 呵呵,这样等于把引号内的sql语句当作字符串赋给TypeID,sql不会执行 网站页面错误? SQL查询语句,页面传递存储问题? asp.net DropDownList回发丢值! 我要重写URL,问个web.config的问题 请教调试问题 .net的网站发布一定要建虚拟目录吗? 如何对EXCEL表的单元格合并,如何读出EXCEL表文件中某个单元格中的内容 一家ASP.NET软件公司的面试题(编程) 客户要求修改,我们这样答复客户,有问题没有 在Asp.net中用C#如何取得服务器端的Ip地址?请教! 类似百度的输入提示怎么实现?用Ajax的哪个控件可以实现? gridview删除确认的问题
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
供参考
SqlCommand cmd=new SqlCommand(TypeID,conn);
string showType=cmd.ExecuteScalar().ToString();
先执行:
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd=new SqlCommand(TypeID,connection );
再赋值:
string TypeID =cmd.ExecuteScalar().ToString();
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
DataSet ds=new Query("select ShowType from CustomerInfo where ID="+newsid+")
string TypeID =ds.table[0].rows[0][ShowType ].tostraing();
string sql= "select ShowType from CustomerInfo where ID="+newsid这样是一个String类型
用法如下:
sqlconnection con=new sqlconnetion('连接数据库字符串');
con.open()
Sqlcommand cmd=new sqlcommand(sql,con);
sqldataadapter adapter=new sqldataadapter(cmd);
DataTable dt=new Datatable();
adapter.fill(dt);
con.close();