日期:2014-05-17  浏览次数:21032 次

List<String> myList = new List<String>()插入数据库
List<String> myList = new List<String>();myList 里面放的ID,但是不知道有多少,现在需要将这些ID放到一张表A的ID字段里面去,表ID字段的数据类型是varchar,请问这个代码该怎么写,希望有完整的连接SqlServer数据库,插入那些的ID的过程,谢谢


------解决方案--------------------
C# code

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myDB;User ID=sa;Password=***");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            foreach (string id in myList)
            {
                cmd.CommandText = string.Format("insert into table(id) values('{0}')", id);
                cmd.ExecuteNonQuery();
            }
            conn.Close();

------解决方案--------------------
C# code

  List<String> myList = new List<String>();
            foreach (string s in myList)
            {
                string sql = "INSERT INTO 表A (id) VALUS('"+s+"')";
                //do sth
            }

------解决方案--------------------
C# code
using (SqlConnection con = new SqlConnection("连接字符串"))
{
    SqlCommand cmd = new SqlCommand("sql语句",con);
    SqlParameter parm = new SqlParameter("@参数名", DbType.String);
    cmd.Parameters.Add(parm);
    con.Open();
    foreach (string s in myList)
    {
        cmd.Parameters["参数名"].Value = s;
        cmd.ExecuteNonQuery();
    }
}

------解决方案--------------------
探讨

C# code

List<String> myList = new List<String>();
foreach (string s in myList)
{
string sql = "INSERT INTO 表A (id) VALUS('"+s+"')";
//do st……

------解决方案--------------------
List<String> myList = new List<String>();
foreach(string s in myList)
{
 //SQLhelper数据库操作类insert
}
------解决方案--------------------
探讨
C# code

List<String> myList = new List<String>();
foreach (string s in myList)
{
string sql = "INSERT INTO 表A (id) VALUS('"+s+"')";
//d……