从asp.net中的storeprocedure获取参数时出错 [英] error in get parameter from storeprocedure in asp.net

查看:73
本文介绍了从asp.net中的storeprocedure获取参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 有人可以回答我吗?我想向数据库中的存储过程发送一个参数.请帮助我
我写了以下代码:

 字符串 aname = TextBox1.Text;
    SqlConnection con =  SqlConnection(" );

        
    SqlCommand cmd =  SqlCommand(" ,con);
    cmd.CommandType = CommandType .StoredProcedure;
    cmd.Parameters.Add(" ,SqlDbType.VarChar)= aname;
        
    SqlDataAdapter ad =  SqlDataAdapter(cmd);
    DataSet ds =  DataSet();
        
    con.Open();
     int 行= cmd.ExecuteNonQuery();
    con .Close();
} 


但我收到2错误
错误1分配的左侧必须是变量,属性或索引器C:\ Documents and Settings \ abarsazan.co \ My Documents \ Visual Studio 2005 \ WebSites \ WebSite12 \ Default.aspx.cs 24 9 C:\ ... \ WebSite12 \
错误2无法将类型``string''隐式转换为``System.Data.SqlClient.SqlParameter''C:\ Documents and Settings \ abarsazan.co \ My Documents \ Visual Studio 2005 \ WebSites \ WebSite12 \ Default.aspx.cs 24 59 C:\ ... \ WebSite12 \

解决方案

更改以下行

 cmd.Parameters.Add(" ,SqlDbType.VarChar )= aname; 





 cmd.Parameters.Add(" ,SqlDbType.VarChar );
cmd.Parameters [" ].Value = aname; 



(或)

 cmd.Parameters.Add(" ,SqlDbType.VarChar ).Value = aname; 



首先,您必须添加参数并指定参数的类型,然后为该参数分配一个值.

请参考此链接 http://msdn.microsoft.com/en-us /library/system.data.sqlclient.sqlcommand.parameters.aspx [ string aname = TextBox1.Text; SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=NezamPezeshki;Integrated Security=True"); SqlCommand cmd = new SqlCommand("InsertActionType", con); cmd.CommandType =CommandType .StoredProcedure ; cmd.Parameters.Add("@Aname", SqlDbType.VarChar) = aname ; SqlDataAdapter ad=new SqlDataAdapter (cmd); DataSet ds=new DataSet (); con.Open(); int rows = cmd.ExecuteNonQuery(); con .Close (); }


but i recieve 2 error
Error 1 The left-hand side of an assignment must be a variable, property or indexer C:\Documents and Settings\abarsazan.co\My Documents\Visual Studio 2005\WebSites\WebSite12\Default.aspx.cs 24 9 C:\...\WebSite12\
Error 2 Cannot implicitly convert type ''string'' to ''System.Data.SqlClient.SqlParameter'' C:\Documents and Settings\abarsazan.co\My Documents\Visual Studio 2005\WebSites\WebSite12\Default.aspx.cs 24 59 C:\...\WebSite12\

Change the following line

cmd.Parameters.Add("@Aname", SqlDbType.VarChar) = aname ;



to

cmd.Parameters.Add("@Aname", SqlDbType.VarChar);
cmd.Parameters["@Aname"].Value = aname;



(or)

cmd.Parameters.Add("@Aname", SqlDbType.VarChar).Value = aname;



First you have to add the parameter and specify the type of parameter and then assign a value to that parameter.

Refer this link
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx[^] for more information.


这篇关于从asp.net中的storeprocedure获取参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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