2016-03-28 13 views
0

我很新的VB,我写了几行代码,但每次运行程序I不断收到我不断收到错误:40当试图保存数据输入到我创建的SQL数据库中,点击一个按钮

provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) 

,这里是我的代码

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click 
    Dim strConnection As String = "Data Source= (local);" & "DataBase='GUU_PATIENT_RECORDDataSet';" & "Integrated Security=yes;" 
    Dim strTreatment_price As String = "INSERT INTO Treatment_price(Patient_Name, " & "Patient Address,Hospital_Number,Phone_Number, Admitted Date,Patients Date Of Birth,Blood Group,Genotype,Treatment1,Treatment2,Treatment3,Treatment4,Treatment5,Treatment6,Price_Unit1,Price_Unit2,Price_Unit3,Price_Unit4,Price_Unit5,Price_Unit6, " & "Sub_Total,VAT,Previous_BAl,NET_Total)" & _ 
    "VALUES ('" & Patient_NameTextBox.Text & "','" & Patient_AddressTextBox.Text & "','" & Hospital_NumberTextBox.Text & "','" & Phone_NumberTextBox.Text & "','" & Blood_Group_TextBox.Text & "','" & GenotypeTextBox.Text & "','" & Treatment1TextBox.Text & "','" & Treatment2TextBox.Text & "','" & Treatment4TextBox.Text & "','" & Treatment5TextBox.Text & "','" & Treatment6TextBox.Text & "','" & Price_Unit1TextBox.Text & "','" & Price_Unit2TextBox.Text & "','" & Price_Unit3TextBox.Text & "','" & Price_Unit4TextBox.Text & "','" & Price_Unit5TextBox.Text & "','" & Price_Unit6TextBox.Text & "','" & Sub_TotalTextBox.Text & "','" & VATTextBox.Text & "','" & Previous_BalTextBox.Text & "','" & Net_TotalTextBox.Text & "');" 

    Using connection As SqlConnection = New SqlConnection(strConnection) 
     Dim command As SqlCommand = New SqlCommand(strTreatment_price, connection) 
     Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from Patient_Payment_info", connection) 
     Dim ds As GUU_PATIENT_RECORDDataSet = New GUU_PATIENT_RECORDDataSet 
     da.Fill(ds.Patient_Payment_Info) 
     Patient_Payment_InfoDataGridView.DataSource = ds.Tables(0) 
     connection.Open() 
     command.ExecuteNonQuery() 
     MsgBox("Record Successfully Validated.") 
    End Using 
+1

这不是创建SQL语句的正确方法。这里最少的研究将展示如何以及为什么你应该使用SQL参数 – Plutonix

+0

很多问题在这里:1)带空格的字段需要用方括号括起来_ [患者地址] _ 2)AVOID绝对字符串连接并使用参数化查询3)Insert Into语句使用SqlCommand.ExecuteNonQuery方法执行。 – Steve

回答

0

线按照下面的步骤。

1. Open control panel. 
2. Search for services. 
3. Open Local services window from the search results 
4. Restart the MSSQLSERVER service. 
+0

好的谢谢,但你能给我一个如何参数化的想法吗? – Chinedu

相关问题