gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:57:06 25 4
gpt4 key购买 nike

我正尝试在我的 .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

我还在 Using Statement 中包含了 SqlConnectionSqlCommand 的创建确保在异常情况下正确关闭和处理连接和命令

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com