SQL Connection String Error: Object reference not set to an instance of an object
SQL Connection String Error: Object reference not set to an instance of an object
我正在尝试文件上传问题的版本 2。这次我想在没有存储过程的情况下测试 SqlDbType
与 OleDbType
。测试此示例时,出现错误
Object reference not set to an instance of an object
这一行:
Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd).ConnectionString
我不明白为什么会出现此错误(第一次使用 SqlDbType
)
我的table类型如下:
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.) >
ConfigurationManager.ConnectionStrings 是系统读取您的应用程序配置文件(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>
当然这意味着您的连接字符串在 app.config 中是硬编码的,因此您无法在 运行 时轻易更改它。
一个可能的选择是 class OracleConnectionStringBuilder
有了这个class就可以写出这样一段代码
Dim builder As New OracleConnectionStringBuilder
builder("Data Source") = strDatabase
builder("User ID") = strUserID
builder("Password") = strPassword
Console.WriteLine(builder.ConnectionString)
我正在尝试文件上传问题的版本 2。这次我想在没有存储过程的情况下测试 SqlDbType
与 OleDbType
。测试此示例时,出现错误
Object reference not set to an instance of an object
这一行:
Dim connection As String = System.Configuration.ConfigurationManager.ConnectionStrings("Provider=""****"";user id=" & strUserID & ";data source=" & strDatabase & ";password=" & strPssWd).ConnectionString
我不明白为什么会出现此错误(第一次使用 SqlDbType
)
我的table类型如下:
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.) >
ConfigurationManager.ConnectionStrings 是系统读取您的应用程序配置文件(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>
当然这意味着您的连接字符串在 app.config 中是硬编码的,因此您无法在 运行 时轻易更改它。
一个可能的选择是 class OracleConnectionStringBuilder
有了这个class就可以写出这样一段代码
Dim builder As New OracleConnectionStringBuilder
builder("Data Source") = strDatabase
builder("User ID") = strUserID
builder("Password") = strPassword
Console.WriteLine(builder.ConnectionString)