string sql="indes ioto teble(id,name) valuse ('"+a+"','"+b+"')";C#里这样的语句 其中A和B可以是变量吗?最好能写个小例子

解决方案 »

  1.   

    string str_Command = "SELECT * FROM users";
                  str_Command = str_Command + " WHERE loginid = '" + strid + "'";
                    str_Command = str_Command + " AND pwd = '" + strpwd + "'";
      

  2.   

    sql = "UPDATE t_prod_purpose SET sortid="+Convert.ToInt32(str_sortid)+",prodname='"+prodname+"',prodSerial='"+prodSerial+"',prodModel='"+
    Func.ReplaceStr(ProdModel.Text)+"',factory='"+Func.ReplaceStr(factory.Text)+"',madeFrom='"+Func.ReplaceStr(MadeFrom.Text)+
    "',costPrice='"+Func.ReplaceStr(CostPrice.Text)+"',MarkPrice='"+Func.ReplaceStr(MarkPrice.Text)+"',MemPrice='"+
    Func.ReplaceStr(MemPrice.Text)+"',is_special='"+str_special+"',specialPrice='"+Func.ReplaceStr(specialPrice.Text)+"',specialfull='"+
    Func.ReplaceStr(due.Text)+"',InfoClsIndex='"+Func.ReplaceStr(ord.Text)+"',is_index='"+str_index+"',is_comm='"+str_comm+
    "',info='"+Func.ReplaceStr(inf.Text)+"',description='"+Func.ReplaceStr(Content.Text)+"'";
      

  3.   

    string a = TextBox1.Text.Replace("'","''");//必须防止注入脚本攻击
    string b =  TextBox2.Text.Replace("'","''");System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
                System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
                cm.Connection = cnn;
                cm.CommandText = "insert into table1 (id) values('"+a+"','"+b+"')";
                
                cnn.Open();
                cm.ExecuteNonQuery();
                cnn.Close();
      

  4.   

    有个现成的VB例子,关于登陆的。
    Dim strSql As String = "Select * From company_user Where userID='" & UserID & "' and userPWD='" & UserPWD & "'"
                '判断用户名或密码是否正确,并登陆
                Dim cmd As New SqlCommand(strSql, conn)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                If dr.Read() <> True Then
                    Response.Write("<script language='javascript'>alert('用户名或密码错误!');</" & "script>")
                Else
                    If dr.GetValue(2) = 1 Then
                        Session("class") = dr.GetValue(2)
                        Response.Redirect("index1.html")
                    Else
                        Session("class") = dr.GetValue(2)
                        Response.Redirect("index2.html")
                    End If
                End If
      

  5.   

    尽管上面的代码可以实现数据插入的操作,但是更好的代码应该是参数的方式:string a = TextBox1.Text;//不必担心注入脚本攻击
    string b =  TextBox2.Text; System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
                System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
                cm.Connection = cnn;
                cm.CommandText = "insert into table1 (field1,field2) values(@field1,@field2)";
                
                cm.Parameters.Add("@field1",SqlDbType.VarChar);
                cm.Parameters["@field1"].Value = a;
                cm.Parameters.Add("@field2",SqlDbType.VarChar);
                cm.Parameters["@field2"].Value = b;
                cnn.Open();
                cm.ExecuteNonQuery();
                cnn.Close();
      

  6.   

    感谢各位大哥对我的帮助。。让我学到了很多知识我实际问题是,我有四十个变量吧。命名规则是ID1,ID2,ID3……NAME1,NAME2……这种规则。我想用循环 只写一条SQL 语句,就把所有的变量的值都存到数据库中…………这样能用变量吗?
    for(int i=1;i++;i<=40)
    {
       string sql="INSERT INTO table (id,name) valuse ('"+ id+1 +"','"+name+1+"')";
     com.ExecuteNonQuery();
    }
    类似上面的东西  请多帮忙
      

  7.   

    老实写代码吧~string[] a = new string[]{id1,id2,id3,...};//至少还是要写一遍
    string[] b = new string[]{name1,name2,name3,...}; System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("连接字符串");
    System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
    cm.Connection = cnn;
    cm.CommandText = "insert into table1 (field1,field2) values(@field1,@field2)";
    cnn.Open();
    for(int i=0;i<a.Length;i++)
    {

    cm.Parameters.Add("@field1",SqlDbType.VarChar);
    cm.Parameters["@field1"].Value = a[i];
    cm.Parameters.Add("@field2",SqlDbType.VarChar);
    cm.Parameters["@field2"].Value = b[i];

    cm.ExecuteNonQuery();
    }
    cnn.Close();
      

  8.   

    string sql="insert into 表名(id,name) values('"+a+"','"+b+"')";
    其中a,b都是变量
    //插入操作
    System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection("server=.;uid=sa;pwd=;database=数据库名");
                System.Data.SqlClient.SqlCommand cm = new System.Data.SqlClient.SqlCommand();
                cm.Connection = cnn;
                cm.CommandText = sql;            
                cnn.Open();
                cm.ExecuteNonQuery();
                cnn.Close();
    这样就可以完成了