1

下面列出的是“.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()
4

2 回答 2

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)
于 2013-03-19T16:21:02.193 回答
-1

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

于 2013-03-19T16:20:35.100 回答