我有一个连接到 SQL Server 的 ASP.Net 网站。在以前的项目(VB)中,我使用以下代码写入我的数据库:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
Dim insertSql As String = "INSERT INTO tblProfile(UserID, UserName, Title, FirstName, LastName, MiddleName, DateofBirth, Gender, HomePhoneNumber, MobilePhoneNumber, Address, StreetName, StreetType, Suburb, PostCode, State, Country) VALUES(@UserID, @UserName, @Title, @FirstName, @LastName, @MiddleName, @DateofBirth, @Gender, @HomePhoneNumber, @MobilePhoneNumber, @Address, @StreetName, @StreetType, @Suburb, @PostCode, @State, @Country)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@UserID", newUserGuid)
myCommand.Parameters.AddWithValue("@UserName", newUserName)
myCommand.Parameters.AddWithValue("@Title", Title.SelectedItem.Text)
myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text)
myCommand.Parameters.AddWithValue("@LastName", LastName.Text)
If MiddleNames.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@MiddleName", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@MiddleName", MiddleNames.Text)
End If
DateofBirth.Text = YearofBirth.Text + "-" + MonthofBirth.Text + "-" + DayofBirth.Text
myCommand.Parameters.AddWithValue("@DateofBirth", DateofBirth.Text)
myCommand.Parameters.AddWithValue("@Gender", Gender.SelectedItem.Text)
If HomePhoneNumber.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@HomePhoneNumber", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@HomePhoneNumber", HomePhoneNumber.Text)
End If
If MobilePhoneNumber.Text = String.Empty Then
myCommand.Parameters.AddWithValue("@MobilePhoneNumber", DBNull.Value)
Else
myCommand.Parameters.AddWithValue("@MobilePhoneNumber", MobilePhoneNumber.Text)
End If
myCommand.Parameters.AddWithValue("@Address", AddressNumber.Text)
myCommand.Parameters.AddWithValue("@StreetName", StreetName.Text)
myCommand.Parameters.AddWithValue("@StreetType", StreetType.SelectedItem.Text)
myCommand.Parameters.AddWithValue("@Suburb", Suburb.Text)
myCommand.Parameters.AddWithValue("@PostCode", Postcode.Text)
myCommand.Parameters.AddWithValue("@State", State.SelectedItem.Text)
myCommand.Parameters.AddWithValue("@Country", Country.SelectedItem.Text)
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
我现在已更改为 C#,并且在更改此代码时遇到了问题。到目前为止,我有:
String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
String insertSql = "INSERT INTO tbl_UserProfiles VALUES(@UserID, @FirstName, @LastName, @YearOfBirth, @Country)";
SqlCommand myCommand = new SqlCommand(insertSql, connectionString);
myCommand.Parameters.AddWithValue("@UserID", newUserGuid);
myCommand.Parameters.AddWithValue("@FirstName", FirstNameTB.Text);
myCommand.Parameters.AddWithValue("@LastName", LastNameTB.Text);
myCommand.Parameters.AddWithValue("@YearOfBirth", YearDDL.SelectedItem.Text);
myCommand.Parameters.AddWithValue("@Country", CountryDDL.SelectedItem.Text);
try
{
connectionString.Open();
myCommand.ExecuteNonQuery();
}
finally
{
connectionString.Close();
}
在查看了一些教程网站和我自己以前的代码后,我尝试创建它。但是,我相信我在这里做错了:
String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
当我得到弯曲的红色下划线时。