Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 使用ASP.Net的SQLServer2005中存在问题_Sql Server 2005 - Fatal编程技术网

Sql server 2005 使用ASP.Net的SQLServer2005中存在问题

Sql server 2005 使用ASP.Net的SQLServer2005中存在问题,sql-server-2005,Sql Server 2005,我使用SQLServer数据库作为后端创建了一个ASP.Net项目。我显示了以下错误。如何解决这个问题 ================编码 Imports System.Data.SqlClient Partial Class Default2 Inherits System.Web.UI.Page Dim myConnection As SqlConnection Dim myCommand As SqlCommand Dim ra As Integ

我使用SQLServer数据库作为后端创建了一个ASP.Net项目。我显示了以下错误。如何解决这个问题

================编码

Imports System.Data.SqlClient

Partial Class Default2

    Inherits System.Web.UI.Page

    Dim myConnection As SqlConnection


    Dim myCommand As SqlCommand
    Dim ra As Integer

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        myConnection = New SqlConnection("Data Source=JANANI-FF079747\SQLEXPRESS;Initial Catalog=new;Persist Security Info=True;User ID=sa;Password=janani") 'server=localhost;uid=sa;pwd=;database=pubs")

        myConnection.Open()

        myCommand = New SqlCommand("Insert into table3 values 'janani','jan'")

        ra = myCommand.ExecuteNonQuery() ========---> error is showing here

        MsgBox("New Row Inserted" & ra)

        myConnection.Close()

    End Sub

End Class 
==========错误消息============

ExecuteNonQuery: Connection property has not been initialized.
创建时,需要将其与连接关联:

myCommand = 
    New SqlCommand("Insert into table3 values 'janani','jan'", myConnection); 
创建时,需要将其与连接关联:

myCommand = 
    New SqlCommand("Insert into table3 values 'janani','jan'", myConnection); 

尝试此操作,以下代码将正确处理所有非托管资源,并且连接已正确初始化:

using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
    using (SqlCommand dataCommand = dataConnection.CreateCommand())
    {
        dataCommand.CommandText = "Insert into table3 values 'janani','jan'"

        dataConnection.Open();
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}

尝试此操作,以下代码将正确处理所有非托管资源,并且连接已正确初始化:

using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
    using (SqlCommand dataCommand = dataConnection.CreateCommand())
    {
        dataCommand.CommandText = "Insert into table3 values 'janani','jan'"

        dataConnection.Open();
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}

+1.我就是这样做的。当我回答上面的问题时,我很懒。希望您能得到接受的答案。@megala您是否用我提供的代码替换了上面的代码?如果没有,请执行此操作并重试。此外,如果再次失败,请发布整个代码和错误消息。+1。我就是这样做的。当我回答上面的问题时,我很懒。希望您能得到接受的答案。@megala您是否用我提供的代码替换了上面的代码?如果没有,请执行此操作并重试。此外,如果再次失败,请发布整个代码和错误消息。