.Net未连接到DB

时间:2013-03-19 16:16:30

标签: .net database connection

下面列出的是“.aspx.vb”文件中的代码。但是,它不会连接到SQL 2008 DB。代码中是否有错误?是否有执行下面列出的存储过程的替代方法?感谢您的预期答案。

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConStr").ToString())
Dim cmd As New SqlCommand

con.Open()
Dim units As TextBox = DirectCast(e.Item.FindControl("txtUnits"), TextBox)
Dim weight As TextBox = DirectCast(e.Item.FindControl("txtWeight"), TextBox)
Dim length As TextBox = DirectCast(e.Item.FindControl("txtLength"), TextBox)
Dim height As TextBox = DirectCast(e.Item.FindControl("txtHeight"), TextBox)
Dim width As TextBox = DirectCast(e.Item.FindControl("txtWidth"), TextBox)
Dim fclass As RadComboBox = DirectCast(e.Item.FindControl("ddlFreightClass"), RadComboBox)

cmd = New SqlCommand("spShipmentsLineValueInsert", con)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(New SqlParameter("@LoadNumber", Convert.ToInt64(Session("loadNumber"))))
cmd.Parameters.Add(New SqlParameter("@Units", Convert.ToInt16(units.Text)))
cmd.Parameters.Add(New SqlParameter("@Weight", Convert.ToDecimal(weight.Text)))
cmd.Parameters.Add(New SqlParameter("@Length", Convert.ToDecimal(length.Text)))
cmd.Parameters.Add(New SqlParameter("@Width", Convert.ToDecimal(width.Text)))
cmd.Parameters.Add(New SqlParameter("@Height", Convert.ToDecimal(height.Text)))
cmd.Parameters.Add(New SqlParameter("@FreightClass", fclass.SelectedItem.Text))
cmd.Parameters.Add(New SqlParameter("@DefaultMeasure", defaultMeasure))
cmd.Parameters.Add(New SqlParameter("@SessionID", userCode))

Dim RetVal As String = cmd.ExecuteNonQuery()

con.Close()

2 个答案:

答案 0 :(得分:1)

你的连接字符串必须是这样的:

ConfigurationManager.ConnectionStrings("ConStr").ConnectionString

示例: 你在web.config中的密钥:

<connectionStrings>
  <clear />
  <add name="ConStr" 
       connectionString=
             "server=MYSERVER; Database=MYDB; Integrated Security=SSPI;" />
</connectionStrings>

从web.config获取连接字符串的代码:

Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConStr").ConnectionString)

答案 1 :(得分:-1)

通常连接字符串看起来像这种模式Data Source= *database path*