从asp.net中的storeprocedure获取参数时出错 [英] error in get parameter from storeprocedure in asp.net
问题描述
我写了以下代码:
字符串 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屋!