1

我有一个连接到 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;

当我得到弯曲的红色下划线时。

4

3 回答 3

2

试试这个....使用SqlConnection

使用 myConnection As New SqlConnection(connectionString) 您没有将此行转换为 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();
            }
于 2012-08-16T11:54:05.750 回答
1

看起来您正在尝试Open()连接字符串:)

翻译

  Using myConnection As New SqlConnection(connectionString)
                    myConnection.Open()
      Dim myCommand As New SqlCommand(insertSql, myConnection)

  using (var myConnection = new SqlConnection(connectionString))
  using (var myCommand = new SqlCommand(insertSql, myConnection))
  {
      myConnection.Open();
      ...
      myCommand.ExecuteNonQuery();
  }

在你的 C# 中使用,SqlConnection并且SqlCommand将保证Dispose()在两个对象上都调用它,无论是成功还是失败(以及关闭连接、清理等)

于 2012-08-16T11:56:57.897 回答
0

确保你有

using System.Configuration 

在文件的顶部。您的 web.config 文件是否还包含配置 > configSections > configSections 部分?

于 2012-08-16T11:57:33.450 回答