Vb.net SQL连接字符串错误:对象引用未设置为对象的实例

Vb.net SQL连接字符串错误:对象引用未设置为对象的实例,vb.net,file-upload,oracle10g,sqlconnection,Vb.net,File Upload,Oracle10g,Sqlconnection,我正在尝试我的文件上传问题的第2版。这次我想测试SqlDbType与OleDbType之间的差异,而不使用存储过程。在测试这个示例时,我得到一个错误 对象引用未设置为对象的实例 在这一行: Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Provider=""****"";user id=" & strUserID & ";data source=" &

我正在尝试我的文件上传问题的第2版。这次我想测试
SqlDbType
OleDbType
之间的差异,而不使用存储过程。在测试这个示例时,我得到一个错误

对象引用未设置为对象的实例

在这一行:

Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd).ConnectionString
我不明白为什么会出现此错误(第一次使用
SqlDbType

我的表格类型如下:

byte: long raw
file_name: varchar2

Protected Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click

  Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName) ' filename extension
  Dim filePath As String = Server.MapPath("~\") 'shows full path
  Dim fullPath As String = filePath + FileUpload1.FileName 'shows full path plus name
  Dim fileName As String = FileUpload1.FileName

  Dim fs As Stream = FileUpload1.PostedFile.InputStream
  Dim br As New BinaryReader(fs)
  Dim bytes As Byte() = br.ReadBytes(fs.Length)    

  Dim strQuery As String = "INSERT into t_classifieds " & "(addate, category, username, phonenbr, email, description, fulldescription, location, byte, file_name)" _
                               & " values ( val_date, val_category, val_user, val_phone, val_email, val_shortDes, val_longDes, val_location, val_byte, val_name )"

        Dim ClassifiedStr As New SqlCommand(strQuery)
        ClassifiedStr.Parameters.Add("val_date", SqlDbType.Date).Value = DateText.Text
        ClassifiedStr.Parameters.Add("val_category", SqlDbType.VarChar).Value = CategoryList.Text
        ClassifiedStr.Parameters.Add("val_user", SqlDbType.VarChar).Value = UserText.Text
        ClassifiedStr.Parameters.Add("val_phone", SqlDbType.VarChar).Value = PhoneText.Text
        ClassifiedStr.Parameters.Add("val_email", SqlDbType.VarChar).Value = EmailText.Text
        ClassifiedStr.Parameters.Add("val_shortDes", SqlDbType.VarChar).Value = ShortText.Text
        ClassifiedStr.Parameters.Add("val_longDes", SqlDbType.VarChar).Value = longText.Value
        ClassifiedStr.Parameters.Add("val_location", SqlDbType.VarChar).Value = LocationText.Text '[8]
        ClassifiedStr.Parameters.Add("val_byte", SqlDbType.Binary).Value = bytes
        ClassifiedStr.Parameters.Add("val_name", SqlDbType.Varchar).Value = fileName
        InsertUpdateData(ClassifiedStr)

End Sub

Private Sub InsertUpdateData(ClassifiedStr As SqlCommand)
    'Throw New NotImplementedException

    Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Provider=""***"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd).ConnectionString

    Dim con As New SqlConnection(connection)

    ClassifiedStr.CommandType = CommandType.Text

    ClassifiedStr.Connection = con

    con.Open()

    ClassifiedStr.ExecuteNonQuery()

    con.Close()

    con.Dispose()
End Sub
当我逐行调试时,我可以看到它正在工作:

? bytes

{Length=9297}
(0): 137
(1): 80
(2): 78
(3): 71
(4): 13
(5): 10
(6): 26
(7): 10
(8): 0
(9): 0
(10): 0
(11): 13
(12): 73
(13): 72
(14): 68
(15): 82
(16): 0
(17): 0
(18): 2
(19): 176
(20): 0
(21): 0
(22): 0
(23): 128
(24): 8
(25): 2
(26): 0
(27): 0
(28): 0
(29): 124
(30): 195
(31): 205
(32): 101
(33): 0
(34): 0
(35): 0
(36): 9
(37): 112
(38): 72
(39): 89
(40): 115
(41): 0
(42): 0
(43): 14
(44): 196
(45): 0
(46): 0
(47): 14
(48): 196
(49): 1
(50): 149
(51): 43
(52): 14
(53): 27
(54): 0
(55): 0
(56): 0
(57): 7
(58): 116
(59): 73
(60): 77
(61): 69
(62): 7
(63): 223
(64): 6
(65): 23
(66): 12
(67): 49
(68): 40
(69): 151
(70): 192
(71): 200
(72): 240
(73): 0
(74): 0
(75): 0
(76): 7
(77): 116
(78): 69
(79): 88
(80): 116
(81): 65
(82): 117
(83): 116
(84): 104
(85): 111
(86): 114
(87): 0
(88): 169
(89): 174
(90): 204
(91): 72
(92): 0
(93): 0
(94): 0
(95): 12
(96): 116
(97): 69
(98): 88
(99): 116
< More... (The first 100 of 9297 items were displayed.) >
?字节
{Length=9297}
(0): 137
(1): 80
(2): 78
(3): 71
(4): 13
(5): 10
(6): 26
(7): 10
(8): 0
(9): 0
(10): 0
(11): 13
(12): 73
(13): 72
(14): 68
(15): 82
(16): 0
(17): 0
(18): 2
(19): 176
(20): 0
(21): 0
(22): 0
(23): 128
(24): 8
(25): 2
(26): 0
(27): 0
(28): 0
(29): 124
(30): 195
(31): 205
(32): 101
(33): 0
(34): 0
(35): 0
(36): 9
(37): 112
(38): 72
(39): 89
(40): 115
(41): 0
(42): 0
(43): 14
(44): 196
(45): 0
(46): 0
(47): 14
(48): 196
(49): 1
(50): 149
(51): 43
(52): 14
(53): 27
(54): 0
(55): 0
(56): 0
(57): 7
(58): 116
(59): 73
(60): 77
(61): 69
(62): 7
(63): 223
(64): 6
(65): 23
(66): 12
(67): 49
(68): 40
(69): 151
(70): 192
(71): 200
(72): 240
(73): 0
(74): 0
(75): 0
(76): 7
(77): 116
(78): 69
(79): 88
(80): 116
(81): 65
(82): 117
(83): 116
(84): 104
(85): 111
(86): 114
(87): 0
(88): 169
(89): 174
(90): 204
(91): 72
(92): 0
(93): 0
(94): 0
(95): 12
(96): 116
(97): 69
(98): 88
(99): 116
<更多。。。(显示了9297项中的前100项。)>
是系统读取应用程序配置文件(app.config或web.config)时返回的字符串数组。您的连接字符串应该存储在那里,并提供一个键属性以从应用程序中检索它

Dim connection As String = ConfigurationManager.ConnectionStrings("myConnection").ConnectionString
在app.config中,您需要有以下行

<connectionStrings>
  <add name="myConnection" connectionString="user id=xxxx;data source=yyyyyy;password=zzzzzz"
  providerName="System.Data.OracleClient" />
</connectionStrings>

SqlConnection
SqlDbType
适用于Microsoft SQL Server,而不是Oracle。。。。对于Oracle,您需要使用
OracleConnection
OracleDbType
:…我使用的是OleDbConnection,但我遇到了问题,所以我尝试切换到SqlConnection。但是我想我应该回到我以前的例子,然后我只看了一下我的web.config,实际上我已经建立了连接字符串。我将名称和我的行
Dim con用作新的SqlConnection(connection)
标记的错误
关键字不受支持:“provider”。
。我不想问,但你熟悉这个问题吗?不知道你的意思,但我会继续研究。谢谢,我想说的是,当在my piece of web.config中粘贴连接字符串作为示例时,插入了不必要的字符串。现在我已经删除了它,您可以在没有提供程序部分的情况下再次测试它。但是您是在使用Oracle还是SqlServer?它是Oracle。我尝试了一个版本,正如你在上所看到的,但经过几个小时的研究后,没有找到任何地方。没有意识到SQLServer就是我正在做的(所谓的)。我想我得回到我的旧版本了。如果您正在使用Oracle,那么您需要引用OracleClient库,并将适当的Imports语句添加到您的程序中。SqlConnection(和family)是一组用于与SQLServer交互的类。Oracle具有相同的类,但前缀为Oracle而不是Sql。Oracle有两个库(一个内置于框架中,另一个直接来自Oracle,具有更多功能),您应该选择其中一个,并且(如果可能)避免使用通用OleDb类。
Dim builder As New OracleConnectionStringBuilder
builder("Data Source") = strDatabase 
builder("User ID") = strUserID 
builder("Password") = strPassword
Console.WriteLine(builder.ConnectionString)