using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SqlConnection sqlcon = new SqlConnection("Data Source= (local);DataBase = Theatre_MS;uid =sa;pwd = 123;"); try { sqlcon.Open();//数据库需要打开 string sqlstr = "insert into User_TB values(‘1‘,‘2‘,‘1‘);"; SqlCommand cmd = new SqlCommand(sqlstr,sqlcon);//须记住cmd的new时需要两个参数,一个是sqlstr,一个是数据库连接---->用来表明是对那个数据库进行那种操作 cmd.ExecuteNonQuery();//executeNonQuery返回是得影响的行数 Console.WriteLine("Yes!"); } catch (SqlException ex) { Console.WriteLine(ex.Message);//输出得是ex。Message } Console.ReadLine(); } } }
原文:http://www.cnblogs.com/silence-tommy/p/4442623.html