以编程方式写入数据库

时间:2012-08-16 11:50:42

标签: 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, 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;

因为我得到了波浪形的红色下划线。

3 个答案:

答案 0 :(得分:2)

试试这个.... 使用SqlConnection

使用myConnection作为新的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();
            }

答案 1 :(得分: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();
  }

SqlConnectionSqlCommand上使用C#将保证在两个对象上都调用Dispose(),无论是成功还是失败(以及关闭连接,清理等)

答案 2 :(得分:0)

确保你有

using System.Configuration 

位于文件顶部。您的web.config文件也包含配置> configSections> configSections部分?