visual studio2008怎么向mdf数据库添加数据呀
能不能来个大佬教一教要怎么用visualstudio2008C#向mdf格式数据库里添加数据呀,,百度找了好多,但是连链接数据库也没弄成功,就是向图里的Northwind...
能不能来个大佬教一教要怎么用visual studio2008C#向mdf格式数据库里添加数据呀,,百度找了好多,但是连链接数据库也没弄成功,就是向图里的Northwind.mdf数据库里添加几条数据,能不能帮忙写一下具体的代码呀,救命了呐~
展开
展开全部
首先建立一个DBHelper类
把这些代码做下修改
class DBHelper
{
// 数据库连接字符串
private string connString = "Data Source=你电脑服务器名称;Initial Catalog=要连接的数据库名称;User ID=sa;Pwd=你安装数据库设置的密码";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
然后在你需要连接数据库的地方实例化DBHelper 类
把这些代码做下修改
class DBHelper
{
// 数据库连接字符串
private string connString = "Data Source=你电脑服务器名称;Initial Catalog=要连接的数据库名称;User ID=sa;Pwd=你安装数据库设置的密码";
// 数据库连接 Connection 对象
private SqlConnection connection;
/// <summary>
/// Connection对象
/// </summary>
public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
return connection;
}
}
/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void CloseConnection()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
然后在你需要连接数据库的地方实例化DBHelper 类
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询