PDA

View Full Version : سوالات یک برنامه نویس مبتدی



sinaexample
جمعه 17 خرداد 1392, 22:05 عصر
آقایون برنامه نویس من بتازگی وی بی رو شروع کردم.
مشکلی تو ثبت اطلاعات یک فرم تو یه جدول تو دیتابیس دارم ممنون میشم راهنماییم کنین
این کدهام:


Imports System.Data.SqlClient
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim prsl As New person With {.name = TextBox1.Text, .family = TextBox2.Text, .tell = TextBox3.Text}
Dim sqlcon1 As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\sql\tes t.mdf;Integrated Security=True;Connect Timeout=30")
Dim sqlc As New SqlCommand("insert into table1(name , family , tell)"
values("prsl.name" , "prsl.family" , "prsl.tell")",sqlcon1)
If sqlcon1.State = ConnectionState.Closed Then
sqlcon1.Open()

End If
sqlc.executenonquery()
End Sub
End Class

sinaexample
جمعه 17 خرداد 1392, 22:10 عصر
در ضمن یه فایل دیگه به نام class1.vb هم دارم که این کدها رو توش نوشتم:

Public Class person
Dim name As String
Dim family As String
Dim tell As Integer
End Class

tstsst
شنبه 18 خرداد 1392, 09:21 صبح
سلام

1) سعی کن از ConnectionString که خود ویژوال استدیو بهت می ده استفاده کنی (بعد از اضافه کردن دیتابیس به پروژه تو پراپرتیس بهت میده) (optional)

2) برای نوشتن یک کد در چند خط مثل زبان های خانواده c یا ... فقط با رفتن به خط بعد نمیشه ادامه داد و باید از & _ استفاده کنی :
Dim sqlc As New SqlCommand("insert into table1(name, family, tell)" & _
"values('" & prsl.name & "','" & prsl.family & "'," & prsl.tell & ")", sqlcon1)

3) باسه شرط های که یک حالت داره نی طونی اینطوری بنویسی البته این به سرعت کمک نمیکنه چون خود if رو vb برات مینویسه فقط کد رو کوتاه میکنه :
If sqlcon1.State = ConnectionState.Closed Then sqlcon1.Open()

4) تو فرمان sql دو ور رشته ها ای ' استفاده کن

('" & prsl.name & "','" & prsl.family & "'," & prsl.tell & ")"5) متغیر هاتو تو دابلکتیشن " نذار.

values("prsl.name" , "prsl.family" , "prsl.tell")6) کدت اینطوری میشه :

Structure person
Dim name As String
Dim family As String
Dim tell As Integer
End Structure
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim prsl As New person With {.name = "test", .family = "test", .tell = 123}
Dim sqlcon1 As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\sql\tes t.mdf;Integrated Security=True;Connect Timeout=30")
If sqlcon1.State = ConnectionState.Closed Then sqlcon1.Open()
Dim sqlc As New SqlCommand("insert into table1(name, family, tell)" & _
"values('" & prsl.name & "','" & prsl.family & "'," & prsl.tell & ")", sqlcon1)
sqlc.ExecuteNonQuery()
End Sub

sinaexample
چهارشنبه 22 خرداد 1392, 01:42 صبح
با تشکر از دوست عزیزم یه مشکل دیگه هم دارم که داره دیوونم میکنه. تو کد زیر



Imports System.Data.SqlClient
Public Class Form1
Dim my_cn As New SqlConnection("Data Source=17SHAHRIVAR-PC\SQLEXPRESS;Initial Catalog=student;Integrated Security=True;Pooling=False")

Dim ds As New DataSet
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
my_cn.Open()
Dim da1 As New SqlDataAdapter("select * from person", my_cn)
Dim da2 As New SqlDataAdapter("select * from kharid", my_cn)
da1.Fill(ds, "person")
da2.Fill(ds, "kharid")
Dim parentcol As DataColumn
parentcol = ds.Tables("person").Columns("code")
Dim childcol As DataColumn
childcol = ds.Tables("kharid").Columns("code_pr")
Dim relation As New DataRelation("rele", parentcol, childcol)
ds.Relations.Add(relation)

For Each parent As DataRow In ds.Tables("person").Rows
Dim nodeparent As TreeNode = TreeView1.Nodes.Add(parent("name").ToString())
For Each child As DataRow In parent.GetChildRows(relation)
nodeparent.Nodes.Add(child("date").ToString)
Next

Next
End Sub





خط ds.Relations.Add(relation) رو همش اخطار میگیره دلیلشم نمیدونم چیه
بازم پیشاپیش از راهنمایتون ممنونم

sinaexample
چهارشنبه 22 خرداد 1392, 14:22 عصر
حل شد.
اخطار به این دلیل بود که تایپ ستونها باهم فرق میکرد.

sinaexample
سه شنبه 04 تیر 1392, 15:03 عصر
دوستان اگه میشه این سوال منو جواب بدید.
سوال بسیار مبتدی هستش.
من چند تا فرم تو یه پروژه دارم. چطور مشخص کنم که کدوم فرم اول اجرا شه؟؟؟
مثلا من اول فرم Frmmember.vb رو درست کردم و بعدش FrmMain.vb. حالا وقتی اجرا میکنم FrmMember.vb اجرا میشه در حالی که من میخوام FrmMain.vb اجرا شه.
چیکار کنم؟؟؟:افسرده:

rm.manesh
سه شنبه 04 تیر 1392, 16:34 عصر
با سلام
در قسمت solution explorer بر روی my project کلیک کن و از تب application از قسمت startup forms میتوانی فرمی را که میخواهی ابتدا لود شود را تعیین کنی