C# 以编程方式写入数据库
我有一个连接到SQL Server的ASP.Net网站。在上一个项目(VB)中,我使用以下代码写入数据库:C# 以编程方式写入数据库,c#,asp.net,sql-server,vb.net,vb.net-to-c#,C#,Asp.net,Sql Server,Vb.net,Vb.net To C#,我有一个连接到SQL Server的ASP.Net网站。在上一个项目(VB)中,我使用以下代码写入数据库: Dim connectionString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString Dim insertSql As String = "INSERT INTO tblProfile(UserID, UserName, Title,
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;
当我看到扭曲的红色下划线时。试试这个。。。。
使用SqlConnection
使用myConnection作为新的SqlConnection(connectionString)您没有将此行转换为C
看起来您正在尝试
Open()
a连接字符串:)
翻译
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
到
在SqlConnection
和SqlCommand
上的C#中使用将保证对这两个对象调用Dispose()
,无论成功与否(以及关闭连接、清理等)确保
using System.Configuration
在文件的顶部。您的web.config文件是否包含configuration>configSections>configSections节?红色下划线是什么意思?很抱歉:与“System.Data.SqlClient.SqlCommand.SqlCommand(string,System.Data.SqlClient.SqlConnection)”匹配的最佳重载方法具有一些无效参数,将myConnection用作新的SqlConnection(connectionString)您没有将此行转换为C#您仍然可以使用连接和命令对象周围的Using语句。第一个代码是我的旧代码,来自一个旧项目。第二个代码是我的新代码,用于此项目。为什么configSections节存在与否很重要?
using (var myConnection = new SqlConnection(connectionString))
using (var myCommand = new SqlCommand(insertSql, myConnection))
{
myConnection.Open();
...
myCommand.ExecuteNonQuery();
}
using System.Configuration