头都大了,高手给看看!
在数据库中写了个search_line的存储过程,两个参数:@Station_Start nvarchar(10),@Station_Stop nvarchar(10)
想在页面中调用这个存储过程,并讲返回的值放到datagrid中!
写了个这样的函数:
public void MyDataGridBind(string Station_Start,string Station_Stop,DataGrid DataGrid1 )
{
SqlConnection myConn=new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand=new SqlCommand( "search_line ",myConn);
//定义访问数据库的方式为存储过程
da.SelectCommand.CommandType = CommandType.StoredProcedure;
//创建访问数据库的参数
SqlParameter parameterstart= new SqlParameter(@Station_Start,SqlDbType.NVarChar,10);
parameterstart.Value=Convert.ToString(Station_Start);
da.SelectCommand.Parameters.Add(parameterstart);
SqlParameter parameterstop= new SqlParameter(@Station_Stop,SqlDbType.NVarChar,10);
parameterstart.Value=Station_Stop;
da.SelectCommand.Parameters.Add(parameterstop);
DataSet ds = new DataSet();
da.Fill(ds, "tab ");
DataColumn mycolumn= ds.Tables[ "tab "].Columns.Add ( "num ",System.Type .GetType ( "System.String "));
for (int i=0;i <ds.Tables[ "tab "].Rows.Count ;i++)
{
ds.Tables[ "tab "].Rows [i][ "num "]=(i+1).ToString ();
}
DataGrid1.DataSource = ds.Tables[ "tab "].DefaultView;
DataGrid1.DataBind();
myConn.Close();
}
页面中测试:
private void Button3_Click(object sender, System.EventArgs e)
{
string str1= "1 ";
string str2= "2 ";
linesearch.MyDataGridBind(str1,str2,this.DataGrid1);
}
提示错误:1不是过程 search_line 的参数。
------解决方案--------------------SqlParameter parameterstart= new SqlParameter(@Station_Start,SqlDbType.NVarChar,10);
SqlParameter parameterstop= new SqlParameter(@Station_Stop,SqlDbType.NVarChar,10);
> > > >
SqlParameter parameterstart= new SqlParameter( "@Station_Start ",SqlDbType.NVarChar,10);
SqlParameter parameterstop= new SqlParameter( "@Station_Stop ",SqlDbType.NVarChar,10);