嗨,我在Webservice中收到错误 [英] Hi Iam getting error in Webservice

查看:103
本文介绍了嗨,我在Webservice中收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是





System.InvalidOperationException:ExecuteNonQuery需要一个开放且可用的连接。连接的当前状态已关闭。

在System.Data.SqlClient.SqlConnection.GetOpenConnection(String方法)

在System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String方法) ,SqlCommand命令)

at System.Data.SqlClient.SqlCommand.ValidateCommand(String method,Boolean async)

at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result ,String methodName,Boolean sendToPipe)

at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

at WebService.db_insert(String UserName,Int32 countryId,String IsActive)in e:\mine \WebSite8 \ App_Code \ WebService.cs:第46行







程序





[WebMethod]

public void db_insert(string UserName,int countryId,string IsActive)

{

SqlConnection con = new SqlConnection(Data Source = SREENI \\ S QLEXPRESS;初始目录= TestDB;用户ID = sa;密码= sa123);

字符串sql = string.Format(@INSERT INTO [Table_1]。[TestDB]

([用户名]

,[countryId]

,[IsActive])

VALUES

(' {0}'

,'{1}'

,'{2}'),UserName,countryId,IsActive);

SqlCommand cmd = new SqlCommand(insert into Table_1 values(@ a,@ b,@ c),con);

cmd.Parameters.AddWithValue(@ a,UserName); cmd.Parameters.AddWithValue(@ b,countryId); cmd.Parameters.AddWithValue(@ c,IsActive); cmd.ExecuteNonQuery();

}

error is


System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WebService.db_insert(String UserName, Int32 countryId, String IsActive) in e:\mine\WebSite8\App_Code\WebService.cs:line 46



programme


[WebMethod]
public void db_insert(string UserName ,int countryId ,string IsActive)
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
string sql = string.Format(@"INSERT INTO [Table_1].[TestDB]
([UserName]
,[countryId]
,[IsActive])
VALUES
('{0}'
,'{1}'
,'{2}')", UserName, countryId, IsActive);
SqlCommand cmd = new SqlCommand("insert into Table_1 values(@a,@b,@c)", con);
cmd.Parameters.AddWithValue("@a", UserName); cmd.Parameters.AddWithValue("@b", countryId); cmd.Parameters.AddWithValue("@c", IsActive); cmd.ExecuteNonQuery();
}

推荐答案

[WebMethod]
public void db_insert(string UserName ,int countryId ,string IsActive)
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
string sql = string.Format(@"INSERT INTO [Table_1].[TestDB]
([UserName]
,[countryId]
,[IsActive])
VALUES
('{0}'
,'{1}'
,'{2}')", UserName, countryId, IsActive);
SqlCommand cmd = new SqlCommand("insert into Table_1 values(@a,@b,@c)", con);
cmd.Parameters.AddWithValue("@a", UserName); 
cmd.Parameters.AddWithValue("@b", countryId); 
cmd.Parameters.AddWithValue("@c", IsActive); 
if(con.State == ConnectionState.Closed)
   con.Open();
cmd.ExecuteNonQuery(); 
con.Close();
} 


这篇关于嗨,我在Webservice中收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆