您的位置:首页 > 数据库

C#连接SqlServer以及插入数据的实例

2014-05-05 20:06 316 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace test2
{
class Program
{
static void Main(string[] args)
{
string connString = "Data Source=127.0.0.1;" +
"Persist Security Info=True;" +
"Initial Catalog=TestSql;" +
"Integrated Security=false;"+
"User ID=sa;"+
"Password=root123;";

SqlConnection conn = new SqlConnection(connString);

try
{
string insertCommand = "insert into employee (username,password) values('LiHua','123')";
conn.Open();
Console.WriteLine("open database successfully!!!");
SqlCommand command = new SqlCommand(insertCommand,conn);
command.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.GetType());
Console.WriteLine("connection Exception!!!");
}
finally
{
conn.Close();
Console.WriteLine("close database successfully!!!");
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: