添加SQLServer服务 视图 - SQL Server 对象资源管理器 - 右键SQL Server - 打开Connect to Server对话框,选填Server name为(localDB)\Projects - connect 新建数据库,表 右键数据库-添加新数据库-数据库名Test-确定 右键表-添加新表test1- 添加列num1,num2-更新 右键dbo.test1-查看表数据-手动添加数据1,2 用sql语句连接数据库
using System
.Data
.SqlClient
;
string connectString
= "Data Source=(LocalDb)\\v11.0; Initial Catalog=test; Integrated Security=true";
SqlConnection conn
= new SqlConnection(connectString
);
try
{
conn
.Open();
MessageBox
.Show("建立连接");
string sql
= "insert into dbo.test1(num1, num2) values ('41', '42')";
SqlCommand cmd
= new SqlCommand(sql
, conn
);
int result
= cmd
.ExecuteNonQuery();
}
catch (SqlException ex
)
{
MessageBox
.Show(ex
.Message
);
}
finally
{
conn
.Close();
}
运行结果