Could I write code similar to:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Class1
Private conn As SqlConnection
Private cmd As SqlCommand
Private sql As String
Public Function ExecuteSQL(ByVal sql As String)
cmd = New SqlCommand(sql, conn)
Try
cmd.ExecuteNonQuery()
Catch Ex As SqlException
MessageBox.Show(Ex.Message.ToString())
End Try
End Function
This method creates a new SQL Server database
Public Function CreateDatabase()
Create a connection
conn = New SqlConnection("Integrated Security=SSPI;" & "Initial Catalog=;" & "Data Source=localhost;")
Open the connection
If Not (conn.State = ConnectionState.Open) Then
conn.Open()
sql = "CREATE DATABASE mydb ON PRIMARY" & "(Name=mydb, filename = E:\Program Files\Microsoft SQL Server\MSSQL\data\mydb.mdf, size=3,"
sql = sql & "maxsize=5, filegrowth=10%)log on"
sql = sql & "(name=mydb_log, filename=E:\Program Files\Microsoft SQL Server\MSSQL\data\mydb_log.ldf,size=3,"
sql = sql & "maxsize=20,filegrowth=1)"
ExecuteSQL(sql)
conn.Close()
End If
End Function
This method creates the tables of the database
Public Function CreateTables()
Open the connection
If Not (conn.State = ConnectionState.Open) Then
conn = New SqlConnection("Integrated Security=SSPI;" & "Initial Catalog=mydb;" & "Data Source=localhost;")
Try
conn.Open()
sql = "CREATE TABLE EmailContacts" & "(ContactID INTEGER NOT NULL CONSTRAINT pk_EmailContacts_ContactID PRIMARY KEY,"
sql = sql & "FirstName NVARCHAR(20), LastName NVARCHAR(35), Email1 NVARCHAR(255))"
ExecuteSQL(sql)
Catch Ex As Exception
MessageBox.Show(Ex.Message.ToString())
End Try
End If
Try
Adding record to the table Emailcontacts
sql = "INSERT INTO EmailContacts(ContactID, FirstName, LastName, Email1)"
sql = sql & "VALUES(1,Rick, Dobson, rickd@cabinc.net)"
ExecuteSQL(sql)
Add second recoord to the table EmailContacts
sql = "INSERT INTO EmailContacts(ContactID, FirstName, LastName, Email1)"
sql = sql & "VALUES(2,Virginia, Dobson, virginia@cabinc.net)"
ExecuteSQL(sql)
conn.Close()
Catch Ex As Exception
MessageBox.Show(Ex.Message.ToString())
End Try
End Function
End Class
for create an Access database with the help of OleDb...
Sorry for my stupid questions...