Programmatically writing to a database
-
26-06-2021 - |
Domanda
I have an ASP.Net website which is connected to an SQL Server. In a previous project (VB) I used the following code to write to my database:
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
I've now changed to C#, and am having problems altering this code. So far I have:
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();
}
Which I've tried to create after looking at a few tutorial sites and my own previous code. But, I believe I'm doing something wrong here:
String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
as I get the squiggly red underline.
Soluzione
It looks like you are trying to Open()
a connection string :)
Translate
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
To
using (var myConnection = new SqlConnection(connectionString))
using (var myCommand = new SqlCommand(insertSql, myConnection))
{
myConnection.Open();
...
myCommand.ExecuteNonQuery();
}
using in C# on your SqlConnection
and SqlCommand
will guarantee that Dispose()
is called on both objects, irrespective of whether success or fail (and close connections, cleanup etc)
Altri suggerimenti
try this....
Use SqlConnection
Using myConnection As New SqlConnection(connectionString) you did not convert this line to C#
String connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnection con = new Sqlconnection(connectionString);
String insertSql = "INSERT INTO tbl_UserProfiles VALUES(@UserID, @FirstName, @LastName, @YearOfBirth, @Country)";
SqlCommand myCommand = new SqlCommand(insertSql, con);
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
{
con.Open();
myCommand.ExecuteNonQuery();
}
finally
{
con.Close();
}
Make sure you have
using System.Configuration
at the top of the file. Also does your web.config file contain a configuration > configSections > configSections section?