【发布时间】:2015-01-20 17:05:32
【问题描述】:
在 Visual Studio Form 中,我正在创建一个 c# 应用程序。我试图将输入到注册表中的数据添加到我用表格创建的 MDF 数据库文件中。但是我无法连接到数据库,有什么帮助吗?
点击时我的注册按钮代码
private void CreateAccBtn_Click(object sender, EventArgs e)
{
string ConnectToDatabase = ("Data Source= (LocalDB)\v11.0;AttachDbFilename=E:\\Work\\Information Systems Development\\Game\\My Brain Cognitive Game\\My Brain Cognitive Game\\Brain Game Database.mdf;Integrated Security=True;Connect Timeout=10");
SqlConnection ConnectDatabase = new SqlConnection(ConnectToDatabase);
SqlCommand CMDDatabase = new SqlCommand("Insert Into Brain Game Database.User Table (User Name, Date Of Birth, Gender, Date Account Created, Condition, Email, Password) values('" + this.NewUsernameTxtBox.Text + "','" + this.DOBDateTimePicker3.Text + "','" + this.SexListBox1.Text + "','" + this.CurrentDatePicker1.Text + "','" + this.NewConditionTxtBox.Text + "','" + this.NewEmailTxtBox.Text + "','" + this.NewPasswordTxtBox.Text + "');",ConnectDatabase);
SqlDataReader MyReader;
try
{
ConnectDatabase.Open();
MyReader = CMDDatabase.ExecuteReader();
MessageBox.Show("Account Created");
while (MyReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
But i cant connect to the database这是什么意思?你得到什么错误信息?你在这里问之前做过什么研究?几乎您可能收到的每条错误消息都已被多次回答和解释。那么是什么让您决定创建一个新问题的具体案例? -
C# ConnectionStrings For Databases 也可以将文件放在较短的文件路径中,只是对这些空格感到好奇
AttachDbFilename=E:\\Work\\Information Systems Development\\Game\\My Brain Cognitive Game\\My Brain Cognitive Game\\Brain Game Database.mdf;我建议在@文字前添加 -
它只是说“建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)
标签: c# visual-studio-2010 sqlconnection mdf