简体   繁体   English

如何将在datagrid中输入的记录保存到sql数据库

[英]how to save records inputted in datagrid to sql database

my system concept is this. 我的系统概念是这样的。 im done displaying columns from different tables using inner join. 我完成了使用内部联接显示来自不同表的列的操作。

im finding a hard time because, i think that you can only use insert command in sql to one table at a time. 即时通讯很难,因为,我认为您一次只能在一个表中使用sql中的insert命令。

but my datagrid displays different columns called from different table. 但是我的数据网格显示从不同表调用的不同列。

how would be the structure of my sql syntax be? 我的sql语法的结构如何? and also calling the stored procedure on vb.net 2003 并在vb.net 2003上调用存储过程

thanks any ideas anyone 谢谢任何想法

THIS IS MY SQL STORED PROC 这是我的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

THIS IS MY VB CODE 这是我的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")

it returns error pointing to 它返回错误指向

 sAdapter.Fill(DataSet)

HERE IS MY SECOND CODE BELOW WITH NO STORED PROCEDURE NEEDED 这是下面的我的第二代码,无需存储程序

    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

but still not working 但仍然无法正常工作

Your code is so long, and by what i have mean is.. 您的代码很长,我的意思是..

 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

i just said about the insert code, beside that you must do by your self. 我只是说过插入代码,除此之外,您必须自己做。 And SQL never have DEFAULT value, you must change the value of your column name to some thing, then add parameter to it that what textbox or field in your form that will be input to your database.. 而且SQL永远不会具有DEFAULT值,您必须将列名的值更改为某些值,然后向其添加参数,以将表单中的哪个文本框或字段输入到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM