Imports System.Data.OleDb
Public Class Form1
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Private Sub cleardata()
idstudent.Clear()
idcitizen.Clear()
End Sub
Private Sub register_bt_Click(sender As Object, e As EventArgs) Handles register_bt.Click
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source =C:\Data_student\registry.xls;extended properties=excel 8.0;"
cn.Open()
With cm
.Connection = cn
.CommandText = "insert into [data registry$] values('" & idstudent.Text & "','" & idcitizen.Text & "')"
.ExecuteNonQuery()
End With
MsgBox("Sucessfully", MsgBoxStyle.Information, Text)
cleardata()
End Sub
Private Sub Namefirst_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Namefirst.KeyPress
End Sub
Private Sub idstudent_KeyPress(sender As Object, e As KeyPressEventArgs) Handles idstudent.KeyPress
If Not (e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = "." Or e.KeyChar = ControlChars.Back) Then
e.Handled = True
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
idstudent.ContextMenuStrip() = New ContextMenuStrip()
idcitizen.ContextMenuStrip() = New ContextMenuStrip()
End Sub
Private Sub idcitizen_KeyPress(sender As Object, e As KeyPressEventArgs) Handles idcitizen.KeyPress
If Not (e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = "." Or e.KeyChar = ControlChars.Back) Then
e.Handled = True
End If
End Sub
Private Sub idstudent_TextChanged(sender As Object, e As EventArgs) Handles idstudent.TextChanged
End Sub
Private Sub idcitizen_TextChanged(sender As Object, e As EventArgs) Handles idcitizen.TextChanged
End Sub
End Class
รู้ึกมันจะถูกเปิดอยู่แล้วนะครับ ให้เช็ค State ก่อนครับ ว่า Open/Close อยู่
Code
If sqlconn.State = ConnectionState.Closed
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source =C:\Data_student\registry.xls;extended properties=excel 8.0;"
cn.Open()
END IF
Date :
2018-07-25 09:13:06
By :
mr.win
No. 2
Guest
ใช้ Using ดีกว่าครับ
ไม่ต้องมานั่งห่วงว่าจะ เปิด หรือ ปิด อยู่
Code (VB.NET)
Using cn As New SqlConnection(ConnectionString)
Try
Dim cmd As SqlCommand = New SqlCommand
With cmd
.Connection = cn
.Connection.Open()
.CommandText = "dbo.GetCustomerByID"
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@CustomerID", SqlDbType.Int, 4)
.Parameters("@CustomerID").Value = CustomerID
End With
da = New SqlDataAdapter(cmd)
da.Fill(ds, "Customer")
Catch ex As Exception
End Try
End Using
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using Oracle.ManagedDataAccess.Client; // ODP.NET (Install-Package odp.net.managed)
using MySql.Data.MySqlClient;
using FirebirdSql.Data.FirebirdClient;
using System.Data.SQLite;
//using WL_DatabaseVB.DbProviderVB;
namespace WL_DatabaseCS
{
public abstract class DbHelper
{
public static string pubConnStr ="" ;
public static string PrefixParam ="";
/// <summary>
/// ctor
/// </summary>
static DbHelper()//public DbHelper()
{
if (pubConnStr == null || pubConnStr == string.Empty)
pubConnStr = WL_DatabaseCS.DbProvider.ConnectionString;
if (PrefixParam == null || PrefixParam == string.Empty)
PrefixParam = WL_DatabaseCS.DbProvider.PrefixParam;
}
public static DataTable Query(string strSQL)
{
DataTable dtReturn = new DataTable();
using (DbConnection Conn = DbProvider.CreateConnection())
{
using (DbDataAdapter da = DbProvider.CreateDataAdapter())
{
using (DbCommand cmd = Conn.CreateCommand())
{
cmd.CommandText = DbProvider.FixedParam(strSQL); // .Replace("'", "''") 'DbProviderVB.FixedParam(strSQL)
da.SelectCommand = cmd;
// If "MsAccess VisualFoxPro".Contains(PubConstant.RDBMSType, StringComparer.CurrentCultureIgnoreCase) Then
Conn.Open(); // FOR ---> MsAccess, Visual FoxPro
// End If
da.Fill(dtReturn);
}
Date :
2018-07-30 21:17:57
By :
หน้าฮี
No. 6
Guest
Class เดียวเอาอยู่ทุกฯ RDBMS
--- ให้ดูของจริงเลยนะ และผ่านการทดสอบข้อมูลจริง > หมื่่นล้านระเบียน มาแล้ว
--- NO SQL / NO Store Procedure /NO NO NO