PDA

مشاهده نسخه کامل : مشکل در کار با Sql در VB.net فوری !!!



mahmoodmahmood
23-09-09, 23:03
سلام دوستان:
لطفان سورس زیر را بررسی کنید و بگید مشکل از کجاست

Imports System.Data
Imports System.Data.SqlClient
Public Class Add_gold_stone_to_backlog
Dim con As New SqlClient.SqlConnection("Data Source=.;Initial Catalog=goldsmith;Integrated Security=True")
Private Sub t1()
Dim n1, n2, n3 As Integer
Dim s1 As String
If RadioButton1.Checked = True Then
n1 = Val(TextBox7.Text)
ElseIf RadioButton2.Checked = True Then
n1 = Val(TextBox2.Text)
End If
n2 = Val(TextBox3.Text)
n3 = n1 * n2
s1 = n3.ToString
TextBox4.Text = s1
End Sub

Private Sub t2()
Dim no1, no2, no3 As Integer
Dim st1 As String
no1 = Val(TextBox4.Text)
no2 = Val(TextBox5.Text)
no3 = no1 - no2
st1 = no3.ToString
TextBox6.Text = st1

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End Sub

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

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
Label1.Enabled = True
Label3.Enabled = True
TextBox1.Enabled = True
TextBox7.Enabled = True
ComboBox1.Enabled = True
Else
Label1.Enabled = False
TextBox7.Enabled = False
Label3.Enabled = False
TextBox1.Enabled = False
ComboBox1.Enabled = False
End If
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
Label2.Enabled = True
Label4.Enabled = True
TextBox2.Enabled = True
ComboBox2.Enabled = True
Else
Label2.Enabled = False
Label4.Enabled = False
TextBox2.Enabled = False
ComboBox2.Enabled = False
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
con.Open()
str = "Insert into [backlog]([b_type],[gold_type],[gold_rate],[stone_type],[stone_price_ghirat],[seller],[b_weight],[b_allsum],[b_pay_sum],[b_remainder_sum])Values('1',@goldt,@goldr,@stonet,@stonep,@seller, @vazn,@allsum,@pay,@remaind) "
Dim cmd As New SqlCommand(str, con)
If (RadioButton1.Checked = True) Then
cmd.Parameters.Add("@goldt", SqlDbType.NVarChar).Value = (ComboBox1.SelectedItem.ToString)
cmd.Parameters.Add("@goldr", SqlDbType.SmallInt).Value = (TextBox1.Text).Trim
cmd.Parameters.Add("@stonet", SqlDbType.NVarChar).Value = 0
cmd.Parameters.Add("@stonep", SqlDbType.Int).Value = 0
End If
If (RadioButton2.Checked = True) Then
cmd.Parameters.Add("@goldt", SqlDbType.NVarChar).Value = 0
cmd.Parameters.Add("@goldr", SqlDbType.SmallInt).Value = 0
cmd.Parameters.Add("@stonet", SqlDbType.NVarChar).Value = (ComboBox2.SelectedItem.ToString)
cmd.Parameters.Add("@stonep", SqlDbType.Int).Value = (TextBox2.Text).Trim
End If
cmd.Parameters.Add("@seller", SqlDbType.NVarChar).Value = (ComboBox3.SelectedItem.ToString)
cmd.Parameters.Add("@vazn", SqlDbType.Int).Value = (TextBox3.Text).Trim
If CheckBox1.Checked = True Then
cmd.Parameters.Add("@allsum", SqlDbType.BigInt).Value = (TextBox4.Text).Trim
cmd.Parameters.Add("@pay", SqlDbType.BigInt).Value = (TextBox5.Text).Trim
cmd.Parameters.Add("@remaind", SqlDbType.BigInt).Value = (TextBox6.Text).Trim
Else
cmd.Parameters.Add("@allsum", SqlDbType.BigInt).Value = 0
cmd.Parameters.Add("@pay", SqlDbType.BigInt).Value = (TextBox5.Text).Trim
cmd.Parameters.Add("@remaind", SqlDbType.BigInt).Value = (TextBox6.Text).Trim
End If
cmd.ExecuteNonQuery()
'End If
con.Close()
MsgBox("success")
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox4.Enabled = True
Else
TextBox4.Enabled = False
End If
End Sub

Private Sub TextBox4_AcceptsTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.AcceptsTabChanged
Call t1()
End Sub

Private Sub TextBox4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Click
Call t1()
End Sub

Private Sub TextBox4_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.EnabledChanged
Call t1()
End Sub

Private Sub TextBox4_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Leave
Call t1()
End Sub

Private Sub TextBox4_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox4.MouseClick
Call t1()
End Sub

Private Sub TextBox4_TabIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TabIndexChanged
Call t1()
End Sub

Private Sub TextBox4_TabStopChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.TabStopChanged
Call t1()
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
Call t1()
End Sub

Private Sub TextBox6_AcceptsTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.AcceptsTabChanged
Call t2()
End Sub

Private Sub TextBox6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.Click
Call t2()
End Sub

Private Sub TextBox6_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.Leave
Call t2()
End Sub

Private Sub TextBox6_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox6.MouseClick
Call t2()
End Sub

Private Sub TextBox6_TabIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.TabIndexChanged
Call t2()
End Sub

Private Sub TextBox6_TabStopChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox6.TabStopChanged
Call t2()
End Sub

Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
Call t2()
End Sub
End Class
وقتی می خوام برنامه رو ثبت کنم به این خط کد گیر میده:

Con.open()
پیغامش هم اینه:


An error has occurred while establishing a connection to the server.
When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
لطفا اشکال سورس رو بهم بگید یا سورس رو درست کنید و بزارین همینجا.
اگه میشه کامل توضیح بدید من خیلی مبتدی هستم

MoBiN.R
24-09-09, 11:06
مشکل Open کردن اتصال در اکثر موارد ، اشتباه در Connection String هست .

شما Connection String رو به این صورت بنویسین :


Dim con As New SqlClient.SqlConnection("server=(local);database=goldsmith;Integrated Security=True")