string TypeID = "select ShowType from CustomerInfo where ID="+newsid; 
这样写有错吗?

解决方案 »

  1.   

    错的!最好是先把查询的值填充到dataset里,在取出值!
      

  2.   

    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;
                }
            }
    供参考
      

  3.   

    你的写法是定义了一个string类型变量,这里是你要执行的操作,可以添加以下方式实现:
    SqlCommand cmd=new SqlCommand(TypeID,conn);
    string showType=cmd.ExecuteScalar().ToString();
      

  4.   

    肯定错了啊~~
    先执行:
    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand cmd=new SqlCommand(TypeID,connection );
    再赋值:
     string TypeID =cmd.ExecuteScalar().ToString();
      

  5.   

    错 语句都没执行就给值了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();
      

  6.   

    set var TypeID ="select ShowType from CustomerInfo where ID="+newsid;sql中赋值用 set 或则 while都可以
      

  7.   

    不好意思看错题目了
    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();
      
      

  8.   

    呵呵,这样等于把引号内的sql语句当作字符串赋给TypeID,sql不会执行