繁体   English   中英

关闭后数据库连接错误,然后再次打开数据源

[英]database connection error after close then open again the data source

我在连接数据库时遇到异常,我在Login.cs文件中打开连接,然后在Select_Item.cs处的openinig连接之后在此处关闭它,但是我仍然收到错误消息:

存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

我在“ 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>

我有两个建议给您:

第一:建立连接时,请使用Login.csusing语句。 请在下面找到代码。

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");
    }
  }
}

2nd:请将连接字符串保留在App.Config或Web.Config中,然后尝试从那里访问它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM