1 string sql ="";//数据库连接 2 SqlConnection conn = new SqlConnection(sql); 3 4 string insertSQL = @"INSERT INTO T_TEST VALUES('"+TextBox1.Text+"')"; 5 6 SqlCommand cmd = new SqlCommand(insertSQL, conn); 7 8 int i = cmd.ExecuteNonQuery();//i>0则说明添加成功
1 public int InsertText(string connectionString, string text) 2 { 3 SqlConnection connection = new SqlConnection(connectionString); 4 string insertSql = "INSERT INTO T_TEST VALUES(@Text)"; 5 SqlCommand command = new SqlCommand(insertSql, connection); 6 SqlParameter parameter = new SqlParameter(); 7 parameter.DbType = DbType.String; 8 parameter.ParameterName = "@Text"; 9 parameter.Value = text; 10 command.Parameters.Add(parameter); 11 return command.ExecuteNonQuery(); 12 }