Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#在类文件中写入连接字符串?_C#_Asp.net_Sql Server_Class - Fatal编程技术网

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

如何使用c#在类文件中写入连接字符串?,c#,asp.net,sql-server,class,C#,Asp.net,Sql Server,Class,我已经在asp.net web应用程序中创建了注册表,并在sqlserver中创建了数据库 我的按钮点击事件是: 因此,我创建了Connection.cs文件,我想在类文件中写入连接字符串,并在按钮单击事件中调用它,这可能吗?像这样尝试 string connectionString ="Your connection string comes here"; using (SqlConnection connection = new SqlConnection(connectionString)

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

我的按钮点击事件是: 因此,我创建了Connection.cs文件,我想在类文件中写入连接字符串,并在按钮单击事件中调用它,这可能吗?

像这样尝试

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

在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();

    }

为什么要在类文件中保留SQL连接字符串?它应该在app.config或web.config文件中。app.config或web.config文件易于访问,因此将其保存在cs页面中可能是安全的,这样其他人就看不到它。在大多数情况下,最好在配置文件中保留连接字符串。对于机密值,请始终使用配置文件。@sanatsathyan,配置文件不容易访问,因为IIS没有提供这些文件。但是,如果安全性是一个问题,连接字符串应该存储在应用程序之外并进行加密。如何阻止有人反编译您的DLL并获取连接字符串?
    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();

    }