C# 操作MySQL,怎样获取到查询语句中的参数值,题目如下: 5
MySqlCommandcmd=newMySqlCommand();cmd.CommandText="select@systemDatetime=SYSDATE()";现...
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "select @systemDatetime = SYSDATE()";
现在的问题是,在只能按照上面这种写法的情况下(可以给cmd对象添加输出参数),我怎样把参数“@systemDatetime”获取到程序中,或者让这个cmd对象能成功的执行(即:调用cmd.ExecuteScalar()方法通过)。反正最终的意思是 怎样通过C# 来执行 “select @systemDatetime = SYSDATE()”这个Sql语句。 展开
cmd.CommandText = "select @systemDatetime = SYSDATE()";
现在的问题是,在只能按照上面这种写法的情况下(可以给cmd对象添加输出参数),我怎样把参数“@systemDatetime”获取到程序中,或者让这个cmd对象能成功的执行(即:调用cmd.ExecuteScalar()方法通过)。反正最终的意思是 怎样通过C# 来执行 “select @systemDatetime = SYSDATE()”这个Sql语句。 展开
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace MyBookShop.DAL
{
public static class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = "Data Source=Lenovo;Initial Catalog=MyBookShop; uid=sa;Password=9527";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
public static int GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandText = "Pro_InsertOrder";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
追问
在我的实际情况中,这个语句不能包含在存储过程中执行,达不到我要的效果。
追答
你想问的是带参数的存储过程输出?output
http://www.cnblogs.com/qiantuwuliang/archive/2009/06/02/1494627.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询