您的位置:首页 > 数据库

C#调用带有参数的SQL存储过程

2013-04-30 08:47 316 查看
public void abc()

{

string a = textBox1.Text.Trim() + "-" + textBox2.Text.Trim() + "-" + textBox3.Text.Trim() + " 20:00:00";

string b = textBox4.Text.Trim() + "-" + textBox5.Text.Trim() + "-" + textBox6.Text.Trim() + " 20:00:00";

try

{

if (DateTime.Parse(a) > DateTime.Parse(b))

{

MessageBox.Show("起始时间需早于终止时间");

return;

}

SqlConnection conn = new SqlConnection("server=10.19.1.59;database=mete_data;uid=sa;pwd=123");

//SqlConnection conn = new SqlConnection("server=.;database=mete_data;Trusted_Connection=yes");

conn.Open();

//调用存储过程

SqlCommand comm = new SqlCommand();

comm.Connection = conn;

comm.CommandText = "XCAllStation_BetweenTwoTime_SumRain";//存储过程的名字

comm.CommandType = CommandType.StoredProcedure;

IDataParameter[] parameters =

{

new SqlParameter("@strStartDatetime", SqlDbType.VarChar,30) , //存储过程中参数的名字

new SqlParameter("@strEndDatetime", SqlDbType.VarChar,30) //存储过程中参数的名字

};

//parameters[0].Value = "2012-7-6";

//parameters[1].Value = "2012-7-7";

parameters[0].Value = a;

parameters[1].Value = b;

comm.Parameters.Add(parameters[0]);

comm.Parameters.Add(parameters[1]);
////直接查询

//SqlCommand comm = new SqlCommand("select * from RAIN_REALTIME",conn);

SqlDataAdapter sda = new SqlDataAdapter();

sda.SelectCommand = comm;

DataSet ds = new DataSet();

sda.Fill(ds);

dataGridView1.DataSource = ds.Tables[0];

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: