如何使用c#在类文件中编写连接字符串?

时间:2018-02-02 04:40:00

标签: c# asp.net sql-server class

我在asp.net web应用程序中创建了注册表单,并在sqlserver中创建了数据库。

我的按钮点击事件是:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string fname = txtFirst.Text;
    string lname = txtLast.Text;
    string middle = txtMiddle.Text;
    string dob = txtDOB.Text;                     
    string add1 = txtAdd1.Text;
    string add2 = txtAdd2.Text;
    string dateofjoin = txtDateofjoin.Text;

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
    con.Open();

    SqlCommand cmd = new SqlCommand("EmployeeRegister", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@firstname", fname);
    cmd.Parameters.AddWithValue("@lastname", lname);
    cmd.Parameters.AddWithValue("@middlename", middle);
    cmd.Parameters.AddWithValue("@dateofbirth", dob);
    cmd.Parameters.AddWithValue("@address1", add1);
    cmd.Parameters.AddWithValue("@address2", add2);
    cmd.Parameters.AddWithValue("@dateofjoin", dateofjoin);
    cmd.Parameters.AddWithValue("@active", CheckActive.Checked ? "1" : "0");
    cmd.ExecuteNonQuery();
    con.Close();                      
}

所以我创建了Connection.cs文件,因为我想在类文件上写连接字符串并在按钮点击事件上调用它,这样可能吗?

2 个答案:

答案 0 :(得分:1)

试试这个

string connectionString ="Your connection string comes here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlCommand cmd = new SqlCommand("EmployeeRegister", connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@firstname", fname);
    cmd.Parameters.AddWithValue("@lastname", lname);
    cmd.Parameters.AddWithValue("@middlename", middle);
    cmd.Parameters.AddWithValue("@dateofbirth", dob);
    cmd.Parameters.AddWithValue("@address1", add1);
    cmd.Parameters.AddWithValue("@address2", add2);
    cmd.Parameters.AddWithValue("@dateofjoin", dateofjoin);
    cmd.Parameters.AddWithValue("@active", CheckActive.Checked ? "1" : "0");
    cmd.ExecuteNonQuery();
}

答案 1 :(得分:0)

在你的connection.cs类中,执行:

public class Connection
{
    public static readonly string ConnectionString = "your connection string here";
}

按下按钮点击事件,如:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string fname = txtFirst.Text;
        string lname = txtLast.Text;
        string middle = txtMiddle.Text;
        string dob = txtDOB.Text;
        string add1 = txtAdd1.Text;
        string add2 = txtAdd2.Text;
        string dateofjoin = txtDateofjoin.Text;

        SqlConnection con = new SqlConnection(Connection.ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand("EmployeeRegister", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@firstname", fname);
        cmd.Parameters.AddWithValue("@lastname", lname);
        cmd.Parameters.AddWithValue("@middlename", middle);
        cmd.Parameters.AddWithValue("@dateofbirth", dob);
        cmd.Parameters.AddWithValue("@address1", add1);
        cmd.Parameters.AddWithValue("@address2", add2);
        cmd.Parameters.AddWithValue("@dateofjoin", dateofjoin);
        cmd.Parameters.AddWithValue("@active", CheckActive.Checked ? "1" : "0");
        cmd.ExecuteNonQuery();
        con.Close();

    }