我正在尝试使用我的.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的新手,所以帮助
答案 0 :(得分:3)
您的代码缺少对数据库中插入数据的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中附加了SqlConnection
和SqlCommand
的创建,以确保在异常的情况下正确关闭和处理连接和命令