使用vb.net插入数据库

时间:2018-07-16 06:26:30

标签: vb.net backend

我有这种形式:

  <div className="form-group">
    <label>First Name</label>
  <input type="text" name="fname" className={`form- 
    control${this.state.fnameMandatory ? ' bg-danger' : ''}`} value= 
    {this.state.fname}  onChange = {this.handleNameChange}/>
     <p className="text-danger">{this.state.fnameMandatory}</p>
    </div>

   <div className="form-group">
   <label>last Name</label>
    <input type="text" name="lname" className={`form- 
       control${this.state.fLnameMandatory ? ' bg-danger' : ''}`} value= 
       {this.state.lname}  onChange = {this.handleLNameChange}/>
      <p className="text-danger">{this.state.fLnameMandatory}</p>
    </div>
<div className="col-xs-6">

        <input type="button" className="btn btn-primary" onClick={this.handleSaveBtn} value="save" />
    </div>

有人可以帮助我如何使用vb.net插入数据库吗?

我做到了:

 Function Save(ByRef _data As String) As String Implements IDataService.Save

    Dim strsql As StringBuilder
    Dim cnsql As SqlConnection
    Dim cmsql As SqlCommand
    Dim myvalue As String

    Try

        cnsql = New SqlConnection(lcConSql)
        cnsql.Open()

        strsql = New StringBuilder

        With strsql
            .Append("INSERT INTO tbl_students (st_firstName, st_lastName, st_gender, st_email) VALUES (// i dont know what to put in here)")
        End With

        cmsql = New SqlCommand(strsql.ToString, cnsql)
我是vb的新手,我不知道如何插入数据库...任何帮助plz?预先感谢

2 个答案:

答案 0 :(得分:0)

您不需要StringBuilder

cmsql.CommandText = "INSERT INTO tbl_students (st_firstName, st_lastName, st_gender, st_email) VALUES (@FirstName,@LastName,@Gender,@Email)"

cmsql.Parameters.AddWithValue("@FirstName", "John")
cmsql.Parameters.AddWithValue("@LastName", "Doe")
cmsql.Parameters.AddWithValue("@Gender", "M")
cmsql.Parameters.AddWithValue("@Email", "johndoe@email.com")

cmsql.ExecuteNonQuery()

答案 1 :(得分:0)

我创建了一个SQL连接器类,以便能够轻松连接到数据库,然后您可以执行命令并从数据库中获取记录。除了SSMS(微软的数据库)外,我没有在其他任何东西上尝试过,但是它应该可以使用。

Imports System.Data.SqlClient

Class sqlConnector
    Private con As SqlConnection
    Public hasSetup As Boolean = False

    Public Sub Setup(ByVal dataSource As String, ByVal initialCatalog As String, ByVal username As String, ByVal password As String)
        Dim connectionString As String = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", dataSource, initialCatalog, username, password)
        con = New SqlConnection(connectionString)

        Try
            con.Open()
            con.Close()
            hasSetup = True
        Catch e As Exception
            Console.WriteLine("There was an error connecting to the server, " & e.Message)
        End Try
    End Sub

    Public Sub Execute(ByVal sqlString As String)
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand(sqlString, con)
        cmd.ExecuteScalar()
        con.Close()
    End Sub

    Public Function GetFirstRow(ByVal sqlString As String) As String()
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand(sqlString, con)
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        reader.Read()
        Dim returnData As String() = New String(reader.FieldCount - 1) {}

        For i As Integer = 0 To reader.FieldCount - 1
            returnData(i) = reader(i).ToString()
        Next

        con.Close()
        Return returnData
    End Function

    Public Function GetSingleValue(ByVal sqlString As String) As String
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand(sqlString, con)
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        reader.Read()
        Dim returnData As String
        returnData = reader(0).ToString()
        con.Close()
        Return returnData
    End Function

    Public Function GetDataTable(ByVal sqlString As String) As DataTable
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand(sqlString, con)
        Dim reader As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim returnData As DataTable = New DataTable()
        reader.Fill(returnData)
        con.Close()
        Return returnData
    End Function

End Class