con.open()中的实例失败 [英] instance failure in con.open()

查看:275
本文介绍了con.open()中的实例失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码



 静态  string  _con = ConfigurationManager.ConnectionStrings [  tours_travel] .ConnectionString; 
SqlConnection con = new SqlConnection(_con);







但是在使用存储过程时出现实例故障错误



 cmd.Connection = con; 





 cmd.CommandText =    usp_packageinsert; 
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ package_name,txtpname.Text);
cmd.Parameters.AddWithValue( @ package_description,txtpdesc.Text);
int check = 0 ;
cmd.Connection = con;
con.Open();
check = cmd.ExecuteNonQuery();
con.Close();
if (check == 1
{
lblmsg。 Visible = true ;
lblmsg.Text = 添加包;
}

解决方案





为什么你首先使用静态字符串变量?

对连接字符串进行编码,如下所示:



 SqlConnection xconn =  new  SqlConnection(ConfigurationManager.ConnectionString [  connectionStrings ]。ToString()); 





其中 connectionStrings 在配置文件中定义。您可能还会在配置文件中检查您的数据源名称。

尝试这些东西然后调试,它应该可以工作。



- 阿努拉格


i have written this code

static string _con = ConfigurationManager.ConnectionStrings["tours_travel"].ConnectionString;
   SqlConnection con = new SqlConnection(_con);




but while using with stored procedure there is an error of instance failure on

cmd.Connection = con;



cmd.CommandText = "usp_packageinsert";
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@package_name", txtpname.Text);
       cmd.Parameters.AddWithValue("@package_description", txtpdesc.Text);
       int check = 0;
       cmd.Connection = con;
       con.Open();
       check = cmd.ExecuteNonQuery();
       con.Close();
       if (check == 1)
       {
           lblmsg.Visible = true;
           lblmsg.Text = "Package added";
       }

解决方案

Hi,

Why are you using static string variable in the first place?
Code your connection string something as below:

SqlConnection xconn = new SqlConnection(ConfigurationManager.ConnectionString["connectionStrings"].ToString());



where connectionStrings is defined in the config file. You might wana also check your Data Source name in the config file.
Try these stuffs and then debug, it should work.

-Anurag


这篇关于con.open()中的实例失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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