【问题标题】:how to save records inputted in datagrid to sql database如何将datagrid中输入的记录保存到sql数据库
【发布时间】:2013-03-07 18:54:37
【问题描述】:

我的系统概念是这样的。我完成了使用内连接显示来自不同表的列。

我发现很难,因为我认为您一次只能在 sql 中使用插入命令到一个表。

但我的数据网格显示从不同表调用的不同列。

我的 sql 语法的结构是怎样的?并在 vb.net 2003 上调用存储过程

感谢任何人的任何想法

这是我的 SQL 存储过程

CREATE PROCEDURE AddToOfficeEquipmentProfile AS

INSERT INTO dbo.tblOfficeEquipmentProfile(OE_ID
                                        , Report_ID
                                        , OE_Category
                                        , OE_SubCategory
                                        , OE_Name
                                        , OE_User
                                        , OE_Brand
                                        , OE_Model
                                        , OE_Specs
                                        , OE_SerialNo
                                        , OE_PropertyNo
                                        , OE_Static_IP
                                        , OE_Vendor
                                        , OE_PurchaseDate
                                        , OE_WarrantyInclusiveYear
                                        , OE_WarrantyStatus
                                        , OE_Status
                                        , OE_Dept_Code
                                        , OE_Location_Code
                                        , OE_Remarks)
VALUES
  (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)
GO

这是我的 VB 代码

Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim Command As SqlCommand = New SqlCommand
    Command.Connection = sqlconn
    Command.CommandText = "AddToOfficeEquipmentProfile"
    Command.CommandType = CommandType.StoredProcedure

    Dim sAdapter As SqlDataAdapter = New SqlDataAdapter(Command)
    Dim DataSet As DataSet = New DataSet(Command.CommandText)

    sAdapter.Fill(DataSet)
    DataGrid1.DataSource = DataSet.Tables(0)
    MsgBox(MsgBoxStyle.OKOnly, "YOU HAVE SUCCESSFULLY ADDED RECORDS TO THE TABLE")

返回错误指向

 sAdapter.Fill(DataSet)

下面是我的第二个代码,无需存储过程

    Dim adapter As New SqlDataAdapter
    Dim sql As String


    sql = "INSERT INTO tblOfficeEquipmentProfile(OE_ID, Report_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo, OE_PropertyNo, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code, OE_Location_Code, OE_Remarks)VALUES(DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)"
    Try
        sqlconn.Open()
        adapter.InsertCommand = New SqlCommand(sql, sqlconn)
        adapter.InsertCommand.ExecuteNonQuery()
        MsgBox("Row inserted !! ")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

还是不行

【问题讨论】:

  • @joshua [code] INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') [/code]
  • 请在你的问题中添加这个,你只插入一个表?
  • 这是一个将记录插入表的示例代码。但我所知道的是你一次只能在一个表中插入记录。但我需要的是在不同的表中插入同时。因为我同时调用了不同表中的列以将其显示在数据网格上
  • 那么你只需添加几个 INSERT 代码然后一次执行它。
  • @ArwinZhang 先生,如果这么简单,我根本不会问问题,但谢谢你的建议

标签: sql vb.net datagrid visual-studio-2003


【解决方案1】:

你的代码太长了,我的意思是..

 Try
                    con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\notifdb.accdb")
                    Dim command As String
                    command = "INSERT INTO NFTB (NOTIF, EMP_NO, EMP_NAME, [POSITION]) VALUES (@NOTIF, @EMP_NO, @EMP_NAME, @POSITION)"
                    con.Open()
                    Dim cmd As OleDbCommand
                    cmd = New OleDbCommand(command, con)
                    cmd.Parameters.AddWithValue("@NOTIF", NOTIFTextBox.Text)
                    cmd.Parameters.AddWithValue("@EMP_NO", EMP_NOTextBox.Text)
                    cmd.Parameters.AddWithValue("@EMP_NAME", EMP_NAMETextBox.Text)
                    cmd.Parameters.AddWithValue("@POSITION", POSITIONTextBox.Text)
                    cmd.ExecuteNonQuery()
                Catch exceptionObject As Exception
                    MessageBox.Show(exceptionObject.Message)
                Finally
                    con.Close()
                End Try

我刚刚说了插入代码,除此之外你必须自己做。 而且 SQL 永远不会有默认值,您必须将列名的值更改为某些东西,然后向其中添加参数,即表单中将输入到数据库的文本框或字段。

【讨论】:

  • 我知道我必须自己做,我只是询问如何修改它的想法。我的意思是代码结构将如何。先生,我会尝试您的代码,谢谢,我会在它工作时将其标记为正确
  • 我用我的 sql 语法替换了 cmd = ...... 但是。错误是蓝线下划线表示“字符串”的值类型无法转换为...
  • 带有“”或什至没有“”,其下划线为蓝线.. cmd = "INSERT INTO tblOfficeEquipmentProfile(OE_ID, Report_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo, OE_Propertyno,OE_Static_IP,OE_VENTOR,OE_PURCHASEDATE,OE_WARRANTYSTATUSYER,OE_WARRANTYSTATUS,OE_STATUS,OE_DEPT_CODE,OE_LOCATION_CODE,OE_REMARKS)值(默认值,默认,默认,默认值,默认值,默认值,默认值,默认值,默认值,默认值,默认值,默认值,默认设置,默认值,默认情况下,默认,默认,默认,默认,默认,默认)"
  • 为什么你的值是默认值?还有,值和列,你只写你要插入什么列,不需要全部...
  • 我在插入 myowntable 名称(列名,...等...保存在我的datagrid中输入的数据并保存到我的sql server?
猜你喜欢
  • 2011-11-16
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多