الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

لدي مشكلة في اضافة صفوف للداتا بيس من نوع Sql Server

بدأه مياوي في 2 أبريل 2009 · 4 رد · 427 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته

أعضاء المنتدى الكريم

أرجو منكم النظر في مشكلتي :

بعد رقم الطالب واسمه في تكست بوكس , واطلب بعد ذلك الاضافة للداتا بيس عن طريق الزر (إضافه) , تظهر لي رسالة بأنه قد تمت اضافة الصفوف , ولكن بعد أن أغلق ال(RUN) , واعود وافتح قاعدة البيانات , لا أجد الصفوف التي قمت باضافتها وقت تشغيل البرنامج ..

ولكن مالمشكلة

frown.gif

الكود :

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

'Declare general variable

Dim con As SqlConnection

Dim Cmd As SqlCommand

Dim ad As New SqlDataAdapter()

Dim myt As New DataTable()

Dim count As Integer = 0

'-------

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

'connect to the database

con = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\Student\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

con.Open()

'establish command object

Cmd = New SqlCommand("Select * from t1", con)

'establish data adapter/data table

ad = New SqlDataAdapter()

ad.SelectCommand = Cmd

myt = New DataTable()

ad.Fill(myt)

'-----------------

Dim r As DataRow

r = myt.NewRow

r = Me.TextBox1.Text

r(1) = Me.TextBox2.Text

myt.Rows.Add

'---------------

Dim adb As New SqlCommandBuilder(ad)

Me.ad.Update(myt)

MessageBox.Show("Inserted ------------ The number of rows=" & myt.Rows.Count)

Me.TextBox1.ResetText()

Me.TextBox2.ResetText()

Catch ex As Exception

MessageBox.Show("Save Error" & ControlChars.CrLf & ex.Message)

End Try

'close the connection

con.Close()

'dispose of the objects

con.Dispose()

Cmd.Dispose()

ad.Dispose()

myt.Dispose()

End Sub

End Class

الرجاء الرد عااااااااااااااااااااااااااااااااجلا وبأقرب وقت frown.gif

شكراً لكم

#2

تكفوووووووووون ردوا .. :(

#3

معقولة ما احد يعرف منكم يا الفريق العربي للبرمجة ؟؟

:(

#4

انظر في هذا الرابط :

http://msdn.microsoft.com/en-us/library/ms233817.aspx

[وسط]لا اله الا انت سبحانك انى كنت من الظالمين

[/وسط]

#5

مرحباً،

قمت بإعادة صياغة الـ( Code ) أعلاه, لم أجربه لأنه ليس لدي نسخة VBE Or VS, أمل أن يعمل, سأحاول تجربته عند عودتي إلى المنزل .

 

Imports [color= #990000;]System[color= #339933;].Data

Imports [color= #990000;]System[color= #339933;].Data[color= #339933;].SqlClient

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class Form1

 

Dim [color= #990000;]Count [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0

[color= #0000ff;]'-------

 

[color= #0000ff;]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBa
se.Load

 

[color= #0000ff;]End Sub

[color= #0000ff;]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B
utton1.Click

[color= #0000ff;]Dim _Connection As SqlConnection

[color= #0000ff;]Dim _Command As SqlCommand

[color= #0000ff;]Dim Adapter As SqlDataAdapter

[color= #0000ff;]Dim _Table As DataTable()

[color= #0000ff;]Try

[color= #0000ff;]'connect to the database

_Connection [color= #339933;]= [color= #000000; font-weight: bold;]New SqlConnection[color= #009900;]([color= #0000ff;]"Data Source=.[color= #000099; font-weight: bold;]SQLEXPRESS;AttachDbFilename=" [color= #339933;]+ Application[color= #339933;].Star
tupPath [color= #339933;]+ [color= #0000ff;]"[color= #000099; font-weight: bold;]Student[color= #000099; font-weight: bold;]Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=Tru
e"[color= #009900;])

_Connection [color= #339933;].Open[color= #009900;]([color= #009900;])

[color= #0000ff;]'establish command object

[color= #0000ff;]_Command = New SqlCommand("Select * From t1", _Connection )

[color= #0000ff;]'establish data adapter[color= #339933;]/data table

Adapter [color= #339933;]= [color= #000000; font-weight: bold;]New SqlDataAdapter[color= #009900;]([color= #009900;])

Adapter[color= #339933;].SelectCommand [color= #339933;]= _Command

_Table [color= #339933;]= [color= #000000; font-weight: bold;]New DataTable[color= #009900;]([color= #009900;])

_Adapter[color= #339933;].Fill[color= #009900;](_Table[color= #009900;])

 

[color= #0000ff;]'-----------------

 

[color= #0000ff;]Dim _Row As DataRow

[color= #0000ff;]_Row = _Table.NewRow()

[color= #0000ff;]_Row(0) = Me.TextBox1.Text

[color= #0000ff;]_Row(1) = Me.TextBox2.Text

[color= #0000ff;]_Table.Rows.Add( _Row )

[color= #0000ff;]_Adapter.Update(_Table )

 

 

[color= #0000ff;]MessageBox.Show("Inserted ------------ The number of rows=" & myt.Rows.Count)

 

[color= #0000ff;]Me.TextBox1.ResetText()

[color= #0000ff;]Me.TextBox2.ResetText()

[color= #0000ff;]Catch Ex As Exception

[color= #0000ff;]MessageBox.Show("Save Error" & ControlChars.CrLf & Ex.Message)

[color= #0000ff;]Finally

[color= #0000ff;]_Connection.Close()

[color= #0000ff;]_Command.Dispose()

[color= #0000ff;]_Adapter.Dispose()

[color= #0000ff;]_Table.Dispose()

[color= #0000ff;]End Try

[color= #0000ff;]End Sub

 

[color= #0000ff;]End Class

بالتوفيق

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

مواضيع مشابهة