2016-01-06 43 views
0

在aspx.vb文件連接字符串的數據源錯誤

Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 

     Dim con As New OleDbConnection("Provider= SQLOLEDB.1;Integrated Security=true; data source=myserver; ") 

     Dim cmd As New OleDbCommand 

     cmd.Connection = con 
     cmd.CommandText = "Insert into demo.dbo.masters1 values ('" & TextBox1.Text & " ," & TextBox4.Text & " , " & TextBox3.Text & " ')" 
     con.Open() 
     If (cmd.ExecuteNonQuery() > 0) Then 
      Label1.BorderColor = Drawing.Color.Aquamarine 
     Else 
      Label1.BorderColor = Drawing.Color.Black 
     End If 

     con.Close() 

End Sub 

我使用Windows身份驗證。

服務器已啓動並正在運行。

我也試過:

Dim con As New OleDbConnection("Provider= SQLOLEDB.1;Integrated Security=true; data source=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\database_name.mdf; ") 

不過,我發現了以下錯誤:

No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).

+0

哪條線會導致錯誤? – techspider

+0

表格母版1有多少列?你想要將一個連接字符串插入一列還是將三個字符串插入三列? –

+0

你迫切需要參數化你的查詢。這是編碼的方式是SQL注入的教科書示例。 –

回答

1

看起來就像你正在使用的OleDbConnection爲SQL Server。

如果您使用SQL Server,您希望SqlConnection。例如,

using(var con = new SqlConnection(
    "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;")) 
{ 
    var query = "insert statement ..."; 
    using(var cmd = new SqlCommand(query, con)){ 
     cmd.CommandType = CommandType.Text; 
     con.Open(); 
     .... 
    } 
} 

您也可以查看連接字符串格式here

FYI:使用參數化查詢,以避免SQL注入攻擊。