我在连接到数据库时遇到异常,我在Login.cs文件中打开连接,然后在此处关闭它,之后在Select_Item.cs上打开连接,但是我仍然收到错误说:
存在具有相同名称的数据库,或者无法打开指定的文件,或者它位于UNC共享上。
我在第4行" con.Open();"
中的Select_Item.cs文件中收到错误Login.cs:
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xchoo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30");
//con.Close();
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From custInfo where Username='" + USERNAME.Text + "'and Password='" + PASSWORD.Text + "'", con);
DataTable custInfo=new DataTable();
//con.Close();
sda.Fill(custInfo);
if (custInfo.Rows[0][0].ToString() == "1")
{
this.Hide();
Select_Item select_item = new Select_Item();
select_item.Show();
con.Close();
}
else
{
MessageBox.Show("Please check your Username and Password");
}
}
Select_Item.cs
private void StoreData()
{
int invoiceID;
using (var con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\choo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30"))
//using (con)
{
con.Open();
//Invoice.Columns.Add(invoiceID);
// invoiceID.AutoIncrement = true;
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"insert into Invoice(invoiceID, subtotal,tax,total) values (1, @subtotal,@tax,@total); select SCOPE_IDENTITY() as InvoiceID;";
cmd.Parameters.AddWithValue("@subtotal", subtotal);
cmd.Parameters.AddWithValue("@tax", tax);
cmd.Parameters.AddWithValue("@total", total);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
invoiceID = (int)reader["InvoiceID"];
}
}
foreach (var item in OrderItems.Rows)
{
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"insert into InvoiceItem(invoiceID,Item_Id,quantity) values (@invoiceID,@Item_Id,@quantity);";
// cmd.Parameters.AddWithValue("@InvoiceID", invoiceID);
cmd.Parameters.AddWithValue("@invoiceID", Convert.ToInt32("invoiceID"));
cmd.Parameters.AddWithValue("@Item_Id", Convert.ToInt32("Item_Id"));
cmd.Parameters.AddWithValue("@quantity", Convert.ToInt32("quantity"));
cmd.ExecuteNonQuery();
}
}
}
}
App.config中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsFormsApplication_test2.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xchoo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30"
providerName="Microsoft.SqlServerCe.Client.4.0" />
<add name="WindowsFormsApplication_test2.Properties.Settings.DataConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xchoo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
答案 0 :(得分:0)
我有两个建议:
1st:请在建立连接时在using
中使用Login.cs
语句。请找到以下代码。
private void button2_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xchoo\Documents\Visual Studio 2012\Projects\WindowsFormsApplication_test2\WindowsFormsApplication_test2\Data.mdf;Integrated Security=True;Connect Timeout=30"))
//con.Close();
{
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From custInfo where Username='" + USERNAME.Text + "'and Password='" + PASSWORD.Text + "'", con);
DataTable custInfo=new DataTable();
//con.Close();
sda.Fill(custInfo);
if (custInfo.Rows[0][0].ToString() == "1")
{
this.Hide();
Select_Item select_item = new Select_Item();
select_item.Show();
con.Close();
}
else
{
MessageBox.Show("Please check your Username and Password");
}
}
}
第二:请将连接字符串保存在App.Config或Web.Config中,并尝试从那里访问它。