c# - ASP.Net-VB 到 .MDF 连接

标签 c# mysql sql asp.net vb.net

我正在尝试在 .net 应用程序中使用 VB 更新我的数据库。我在 app_code 文件夹中添加了一个 data_Set 文件,并将数据库上传到其中。我似乎无法更新表并向其中插入新行。 (我是数据库新手)数据库文件是.mdf,我按照数据访问层的说明进行操作(生成的sql语句)。

这是我的 web.config 文件:

<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CreatePatient.mdf;Integrated Security=True;User Instance=True"
      providerName="System.Data.SqlClient" />
    <add name="CreatePatientConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CreatePatient.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
  </system.web>
</configuration>

这是我的 VB:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ErrorTXT.Visible = False
    End Sub

    Protected Sub SignUp_Click(sender As Object, e As System.EventArgs) Handles SignUp.Click

        Dim CCSQL As New SqlConnection
        Dim CCUser As New SqlCommand
        Dim strSQL As String
        If IsValid Then
            Try
                CCSQL.ConnectionString = ConfigurationManager.ConnectionStrings("CreatePatientConnectionString").ConnectionString
                strSQL = "insert into CreatePatient  (Account, Password, FirstName, LastName, Address, HomePhone, Mobile, City, State, Zip, Social, Age, Gender, Height, Weight, Marital, Spouse) values (@Account, @Password, @FirstName, @LastName, @Address, @HomePhone, @Mobile, @City, @State, @Zip, @Social, @Age, @Gender, @Height, @Weight, @Marital, @Spouse)"
                CCUser.CommandText = strSQL
                CCUser.CommandType = Data.CommandType.Text
                CCUser.Parameters.Add("@Account", Data.SqlDbType.VarChar).Value = Account.Text
                CCUser.Parameters.Add("@Password", Data.SqlDbType.VarChar).Value = Password.Text
                CCUser.Parameters.Add("@FirstName", Data.SqlDbType.VarChar).Value = FirstName.Text
                CCUser.Parameters.Add("@LastName", Data.SqlDbType.VarChar).Value = LastName.Text
                CCUser.Parameters.Add("@Address", Data.SqlDbType.VarChar).Value = Address.Text
                CCUser.Parameters.Add("@HomePhone", Data.SqlDbType.VarChar).Value = HomePhone.Text
                CCUser.Parameters.Add("@Mobile", Data.SqlDbType.VarChar).Value = Mobile.Text
                CCUser.Parameters.Add("@City", Data.SqlDbType.VarChar).Value = City.Text
                CCUser.Parameters.Add("@State", Data.SqlDbType.VarChar).Value = State.Text
                CCUser.Parameters.Add("@Zip", Data.SqlDbType.Int).Value = ZipCode.Text
                CCUser.Parameters.Add("@Social", Data.SqlDbType.VarChar).Value = SSN.Text
                CCUser.Parameters.Add("@Age", Data.SqlDbType.Int).Value = CurrentAge.Text
                CCUser.Parameters.Add("@Gender", Data.SqlDbType.VarChar).Value = Gender.SelectedValue
                CCUser.Parameters.Add("@Height", Data.SqlDbType.VarChar).Value = Height.Text
                CCUser.Parameters.Add("@Weight", Data.SqlDbType.VarChar).Value = Weight.Text
                CCUser.Parameters.Add("@Marital", Data.SqlDbType.VarChar).Value = Marital.SelectedValue
                CCUser.Parameters.Add("@Spouse", Data.SqlDbType.VarChar).Value = Spouse.Text
                CCSQL.Open()
                CCUser.Connection = CCSQL
                Response.Redirect("ProcedureSelectionForm.aspx")
                CCSQL.Close()
            Catch ex As Exception
                ErrorTXT.Text = ex.Message
            End Try
        End If
    End Sub
End Class

这个想法是填写帐户凭据并将其存储在数据库中。如果我遗漏任何信息,请告诉我。我是 stackoverflow、ASP.net 和 SQL 的新手,因此需要帮助

最佳答案

您的代码缺少对 ExecuteNonQuery 的调用将数据插入数据库

If IsValid Then
    Try
        Dim strSQL = "insert into CreatePatient  (Account, Password, FirstName, LastName, " & _
                 "Address, HomePhone, Mobile, City, State, Zip, Social, Age, Gender, " & _
                 "Height, Weight, Marital, Spouse) values (@Account, @Password, @FirstName, " & _
                 "@LastName, @Address, @HomePhone, @Mobile, @City, @State, @Zip, @Social, " & _
                 "@Age, @Gender, @Height, @Weight, @Marital, @Spouse)"
        Using CCSQL = new SqlConnection(ConfigurationManager.ConnectionStrings("CreatePatientConnectionString").ConnectionString)
        Using CCUser = new SqlCommand(strSQL, CCSQL)
            CCSQL.Open()
            CCUser.Parameters.Add("@Account", Data.SqlDbType.VarChar).Value = Account.Text
            ......
            CCUser.ExecuteNonQuery()
        End Using
        End Using
        Response.Redirect("ProcedureSelectionForm.aspx")
    Catch ex As Exception
        ErrorTXT.Text = ex.Message
    End Try
End If

我还将 SqlConnectionSqlCommand 的创建包含在 Using Statement 中。确保在出现异常时正确关闭和处理连接和命令

关于c# - ASP.Net-VB 到 .MDF 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25827922/

相关文章:

c# - Moq - 在设置的返回中使用 It.IsAny 时会发生什么?

c# - 使用 SqlDataReader 将数据行返回到 List<string>

php - Codeigniter 3 从表中选择全部但排除当前组 ID

带有 mysql 数据库的 C# 应用程序,日期保存为字符串

mysql - 给定第一个查询后,最常见的第二个查询 - SQL 分组

c# - 获取 CodeDomProvider 实例的正确方法

c# - 在以下案例实例中,我应该使我的 SQL 查询异步吗?

mysql - 分页统计报告

java - h2|未找到 Java 列(但语句在其他地方有效)

mysql - Select max 在 MySQL 中返回错误值