c#如何用select count统计记录数并赋值给变量?又如何输出统计数变量?
1个回答
展开全部
可用下面的方法:
存储过程+c#代码调用。
示例代码:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "server=sqlServerName;user id=userID;" +
"password= yourPwd;initial catalog=dbName";
DataSet MyDataSet = new DataSet();
System.Data.SqlClient.SqlDataAdapter DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
if (myConnection.State != ConnectionState.Open)
{
myConnection.Open();
}
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("Pro_GetStudentInfo", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
//添加输入查询参数、赋予值
myCommand.Parameters.Add("@myName", SqlDbType.VarChar);
myCommand.Parameters["@myName"].Value = "li";
//添加输出参数
myCommand.Parameters.Add("@Rowcount", SqlDbType.Int);
myCommand.Parameters["@Rowcount"].Direction = ParameterDirection.Output;
myCommand.ExecuteNonQuery();
DataAdapter.SelectCommand = myCommand;
if (MyDataSet != null)
{
DataAdapter.Fill(MyDataSet, "table");
}
this.dataGridView1.DataSource = MyDataSet.Tables[0];
//得到存储过程输出参数
MessageBox.Show( myCommand.Parameters["@Rowcount"].Value.ToString());
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
}
建库脚本:
create table student
(
id varchar(20),
name varchar(50)
);
go
insert into student values('1','wang');
insert into student values('2','li');
insert into student values('3','li');
go
CREATE PROC Pro_GetStudentInfo
@myName VARCHAR(20),
@Rowcount INT OUTPUT
AS
BEGIN
set @Rowcount=( SELECT count(*) FROM student WHERE name=@myName);
SELECT * FROM student WHERE name=@myName;
END
GO
存储过程+c#代码调用。
示例代码:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "server=sqlServerName;user id=userID;" +
"password= yourPwd;initial catalog=dbName";
DataSet MyDataSet = new DataSet();
System.Data.SqlClient.SqlDataAdapter DataAdapter = new System.Data.SqlClient.SqlDataAdapter();
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connectionString);
if (myConnection.State != ConnectionState.Open)
{
myConnection.Open();
}
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("Pro_GetStudentInfo", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
//添加输入查询参数、赋予值
myCommand.Parameters.Add("@myName", SqlDbType.VarChar);
myCommand.Parameters["@myName"].Value = "li";
//添加输出参数
myCommand.Parameters.Add("@Rowcount", SqlDbType.Int);
myCommand.Parameters["@Rowcount"].Direction = ParameterDirection.Output;
myCommand.ExecuteNonQuery();
DataAdapter.SelectCommand = myCommand;
if (MyDataSet != null)
{
DataAdapter.Fill(MyDataSet, "table");
}
this.dataGridView1.DataSource = MyDataSet.Tables[0];
//得到存储过程输出参数
MessageBox.Show( myCommand.Parameters["@Rowcount"].Value.ToString());
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
}
建库脚本:
create table student
(
id varchar(20),
name varchar(50)
);
go
insert into student values('1','wang');
insert into student values('2','li');
insert into student values('3','li');
go
CREATE PROC Pro_GetStudentInfo
@myName VARCHAR(20),
@Rowcount INT OUTPUT
AS
BEGIN
set @Rowcount=( SELECT count(*) FROM student WHERE name=@myName);
SELECT * FROM student WHERE name=@myName;
END
GO
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询