我一直在尝试连接到数据库,但它显示“空”错误。所以,我尝试打开我之前练习的表格,结果非常好。
这是我的代码:
string username = txtusername.Text;
string firstname = txtfirstname.Text;
string lastname = txtlastname.Text;
string email = txtemail.Text;
string password = txtpass.Text;
string gender = rbgender.Text;
string nationality = dcountry.Text;
string phone = txtphone.Text;
string connection = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection connect = new SqlConnection(connection);
string command = "INSERT INTO Persons(username, firstname, lastname, email, password, gender, nationality, phone) VALUES('@username','@firstname','@lastname','@email','@password','@gender','@nationality','@phone')";
SqlCommand insert = new SqlCommand(command +","+ connection);
insert.Parameters.AddWithValue("@username", username);
insert.Parameters.AddWithValue("@firstname", firstname);
insert.Parameters.AddWithValue("@lastname", lastname);
insert.Parameters.AddWithValue("@email", email);
insert.Parameters.AddWithValue("@password", password);
insert.Parameters.AddWithValue("@gender", gender);
insert.Parameters.AddWithValue("@nationality", nationality);
insert.Parameters.AddWithValue("@phone", phone);
insert.ExecuteNonQuery();
connect.Close();
它与 web.config 中的连接字符串有什么关系吗?
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=ADRIAN-LAPTOP\SQL;Initial Catalog=New;Integrated Security=True"
providerName="System.Data.SqlClient" />