需要帮忙 !!数据未保存在sql server数据库中。 [英] Need help !! Data is not being saved on the sql server database.

查看:116
本文介绍了需要帮忙 !!数据未保存在sql server数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
    Dim cnn As New SqlConnection
    Dim cmd As New SqlCommand
   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cmd.Connection = cnn
        cmd = New SqlCommand("insert into info(id, address) values('" & TextBox1.Text & "','" & TextBox2.Text & "')", cnn)
        cmd.ExecuteNonQuery()
        MsgBox("data saved successfully.")

        cnn.Close()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath & "\Database1.mdf; Integrated Security=True;User Instance=True"
        cnn.Open()
    End Sub
End Class









我是否需要添加更多数据才能将数据从文本框保存到数据库?





Do I need to add more to save the data from the textboxes to database?

推荐答案

0)请把所有的数据访问代码在一个单独的类 - 数据访问层。

1)请使用参数化查询; 使用连接来形成您的查询。

2)不要打开连接并在申请期间保持打开状态。

3)使用try / finally

4)你的点击将关闭连接,第二次点击将失败。



除此之外,它看起来很好。
0) Please put all the data access code in a separate class -- a Data Access Layer.
1) Please use a parameterized query; do not use concatenation to form your queries.
2) Do not open the connection and leave it open for the duration of the application.
3) Use a try/finally
4) Your click will close the connection and a second click will fail.

Other than that, it looks fine.


这篇关于需要帮忙 !!数据未保存在sql server数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆