日期:2014-05-17 浏览次数:21014 次
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();
------解决方案--------------------
List<String> myList = new List<String>(); foreach (string s in myList) { string sql = "INSERT INTO 表A (id) VALUS('"+s+"')"; //do sth }
------解决方案--------------------
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(); } }
------解决方案--------------------