• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

چگونگی ارتباط با دیتابیس در دات نت

payam.khaleghi

کاربر تازه وارد
تاریخ عضویت
26 دسامبر 2007
نوشته‌ها
10
لایک‌ها
0
سلام من تا حدی با sql و Vb.net آشنایی دارم ولی متاسفانه مرجع کامل و خوبی رو برای ارتباط بین این دو پیدا نمی کنم.لطفا مرجع و راهنمایی رو با مثال های خوب برای این موضوع به من معرفی کنید.
متشکرم.;)
 

H_R

مدیر بازنشسته
تاریخ عضویت
30 مارس 2005
نوشته‌ها
3,298
لایک‌ها
17
سن
43
محل سکونت
North Pole
سلام
ساختن جدول :
PHP:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace SimpleSql
Class CreateTable
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Shared<STAThread()>_
Sub Main(ByVal args() As String)
' Make a SqlConnection and the Connection string as an argument
Dim conn As New SqlConnection("Data Source=computer_name;" + "Initial Catalog=database_name;" + "User ID=sa;" + "Password=pass;")
' Next thing we will do is make a sqlcommand so 
'* we can exucute our query'
Dim cmd As New SqlCommand
' We could give this as argument but that won't make our code real clear
cmd.CommandTimeout = 60 '
cmd.Connection = conn ' Sets wich connection you want to use
cmd.CommandType = CommandType.Text ' Sets the command type to use
cmd.CommandText = "CREATE TABLE simplesql " + "(" + "simple_id int," + "simple_text text" + ")"
' Why this approach you could write it in one line right???
'* Yes you can you coudl make it like this
'* "CREATE TABLE simplesql ( simple_id int, simple_text text )";
'* most people would do it the simple way inclusive me :)
'* but I want to make it real clear how to do it right '
' Next We will open the connection and make the query
Try
conn.Open() ' Open the connection if it fails 
' it will let you know with an exception
If conn.State = ConnectionState.Open Then
' Execute the query for this nothing is returned
cmd.ExecuteScalar()
Console.WriteLine("Table is Created")
End If
Catch exp As Exception
Console.Write(exp.Message)
Finally
conn.Close() ' close the connection
End Try
Console.WriteLine("Press any key to quite")
Console.Read()
End Sub 'Main
End Class 'CreateTable
End Namespace 'SimpleSql

ورود اطلاعات به جدول ( Insert ):

PHP:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace SimpleSql1
Class Insert
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Shared<STAThread()> 
Sub Main(args() As String)
Dim conn As New SqlConnection("Data Source=Computername;" + "Initial Catalog=simplesql;" + "User ID=sa;" + "Password=pass;") 
' TODO: change comp name
Dim strText As String = Console.ReadLine()
Try
Dim cmd As New SqlCommand
cmd.CommandTimeout = 60
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT Count(*) FROM simplesql" 
' Couldn't do it without sorry
conn.Open()
If conn.State = ConnectionState.Open Then
Dim objCount As Object = cmd.ExecuteScalar()
Dim iCount As Integer = CInt(objCount)
Console.WriteLine("Count was succesfull")
cmd.CommandText = "INSERT INTO simplesql (simple_id, simple_text) VALUES (" + iCount + ",'" + strText + "')"
cmd.ExecuteScalar()
Console.WriteLine("Succesfully inserted the string")
End If
Catch exp As Exception
Console.Write(exp.Message)
Finally
conn.Close()
End Try
Console.WriteLine(ControlChars.Lf + ControlChars.Lf + " Press any key to quite")Console.Read()
End Sub 'Main
End Class 'Insert
End Namespace 'SimpleSql1

انتخاب (select):
PHP:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace SimpleSql1
Class [Select]
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
<STAThread()> _Overloads Shared Sub Main(ByVal args() As String)
' we will first do the same thing 
'* connecting to the database as we did before'
Dim conn As New SqlConnection("Data Source=computer_name;" + "Initial Catalog=simplesql;" + "User ID=sa;" + "Password=pass;")
Dim dr As SqlDataReader
Try
Dim cmd As New SqlCommand
cmd.CommandTimeout = 60
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT Count(*) FROM simplesql"
conn.Open()
If conn.State = ConnectionState.Open Then
Dim objCount As Object = cmd.ExecuteScalar()
Dim iCount As Integer = CInt(objCount)
Console.WriteLine("Count was succesfull " + ControlChars.Lf)
cmd.CommandText = "SELECT simple_id, simple_text FROM simplesql ORDER BY simple_id"
dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
Console.WriteLine("Succesfully Selected " + ControlChars.Lf)
' For loop to read everything from the table
Dim i As Integer
For i = 0 To iCount - 1
dr.Read() ' Read one row from the table
Console.WriteLine("ID: {0} " + ControlChars.Tab + " Text: {1}", dr(0), dr(1))
Next i
End If
Catch exp As Exception
Console.Write(exp.Message)
Finally
conn.Close()
End Try
Console.WriteLine(ControlChars.Lf + ControlChars.Lf + " Press any key to quite")
Console.Read()
End Sub 'Main
End Class '[Select]
End Namespace 'SimpleSql1

بدین صورت !
 

aminbrlee

Registered User
تاریخ عضویت
18 سپتامبر 2007
نوشته‌ها
115
لایک‌ها
15
کد وصل شدن به دیتابیس
کد:
SqlConnection connect = new SqlConnection(str_con);
            connect.Open();
به جای str_con رشته کانکشن رو وارد کنید
 
بالا