c#如何获取数据库中某个表中的其中一个字段的值 15
展开全部
给你一个获得单值的类:(其中的
strconn是你的数据库连接字符串)//*****************************************************************************************//
/// <summary>
/// 从数据库中检索单个非数值型数据
/// </summary>
/// <author name="sky"></author>
/// <createtime value="2002/08/03"></createtime>
/// <param name="strSQL"></param>
/// <returns></returns>
public static string GetStr(string strSQL)
{
string strTmp="";
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
strTmp = Convert.ToString(objCmd.ExecuteScalar());
objConn.Close();
return strTmp;
}
//*****************************************************************************************//
/// <summary>
///从数据库中检索单个数值型数据
/// </summary>
/// <author name="sky"></author>
/// <createtime value="2002/08/03"></createtime>
/// <param name="strSQL"></param>
/// <returns></returns>
public static int GetInt(string strSQL)
{
int intTmp;
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
intTmp = Convert.ToInt32(objCmd.ExecuteScalar()==null?0:objCmd.ExecuteScalar());
objConn.Close();
return intTmp;
}
/// <summary>
///从数据库中检索单个数值型数据
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static long GetLong(string strSQL)
{
long nRet;
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
nRet = Convert.ToInt64(objCmd.ExecuteScalar());
objConn.Close();
return nRet;
}
strconn是你的数据库连接字符串)//*****************************************************************************************//
/// <summary>
/// 从数据库中检索单个非数值型数据
/// </summary>
/// <author name="sky"></author>
/// <createtime value="2002/08/03"></createtime>
/// <param name="strSQL"></param>
/// <returns></returns>
public static string GetStr(string strSQL)
{
string strTmp="";
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
strTmp = Convert.ToString(objCmd.ExecuteScalar());
objConn.Close();
return strTmp;
}
//*****************************************************************************************//
/// <summary>
///从数据库中检索单个数值型数据
/// </summary>
/// <author name="sky"></author>
/// <createtime value="2002/08/03"></createtime>
/// <param name="strSQL"></param>
/// <returns></returns>
public static int GetInt(string strSQL)
{
int intTmp;
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
intTmp = Convert.ToInt32(objCmd.ExecuteScalar()==null?0:objCmd.ExecuteScalar());
objConn.Close();
return intTmp;
}
/// <summary>
///从数据库中检索单个数值型数据
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static long GetLong(string strSQL)
{
long nRet;
OleDbConnection objConn = new OleDbConnection(strConn);
OleDbCommand objCmd = objConn.CreateCommand();
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
nRet = Convert.ToInt64(objCmd.ExecuteScalar());
objConn.Close();
return nRet;
}
展开全部
一段代码:直接粘贴过去就可以了
private string get(string tablname, string column, string text, string selcolumn)//tablname数据库表名 column选择的列名 selcolumn 限制的列名 text 限制的内容
{
using(SqlConnection conn = new SqlConnection(source))//source是连接字符串
{
try
{
conn.Open();
string cmdtext = @"select top 1 " + selcolumn + " from " + tablname + " where " + column + " = '" + text + "'";
SqlCommand cmd = new SqlCommand(cmdtext, conn);
return Convert.ToString(cmd.ExecuteScalar());
}
catch(Exception ex)
{
return "something error!! ";
}
}
}
private string get(string tablname, string column, string text, string selcolumn)//tablname数据库表名 column选择的列名 selcolumn 限制的列名 text 限制的内容
{
using(SqlConnection conn = new SqlConnection(source))//source是连接字符串
{
try
{
conn.Open();
string cmdtext = @"select top 1 " + selcolumn + " from " + tablname + " where " + column + " = '" + text + "'";
SqlCommand cmd = new SqlCommand(cmdtext, conn);
return Convert.ToString(cmd.ExecuteScalar());
}
catch(Exception ex)
{
return "something error!! ";
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection con=new SqlConnection("连接字符串");
SqlDataAdapter sda=new SqlDataAdapter("SQL语句",con);
DataSet ds=new DataSet();
sda.Fill(ds);
string val=ds.Table[0].Rows[第几行]["列名"].ToString();
SqlDataAdapter sda=new SqlDataAdapter("SQL语句",con);
DataSet ds=new DataSet();
sda.Fill(ds);
string val=ds.Table[0].Rows[第几行]["列名"].ToString();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//数据库连接字符串
private string str1 = "Provider=SQLOLEDB;Server=(local);DataBase=ab;uid=abc;pwd=123";
//数据查询的SQL语句。这是根据需要写的SQL语句
private string strsql = "select name from bs";
public DataSet sqlexecutesql4ds(string strsql,string str1)
{
using (SqlConnection mycn = new SqlConnection(str1))
{
mycn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(strsql, mycn))
{
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
}
}
//然后调用sqlexecutesql4ds函数
DataSet ds = new DataSet();
ds = sqlexecutesql4ds(strsql,str1);
string values = " ";
for(int i = 0;i < ds.Tables[0].Rows.Count; i++)
{
values = values + ds.Tables[0].Rows[i][0].ToString() + "<br>";
}
ds.Dispose();
注意以上代码修改下数据库连接。修改下你查询的SQL语句就直接可以用。
private string str1 = "Provider=SQLOLEDB;Server=(local);DataBase=ab;uid=abc;pwd=123";
//数据查询的SQL语句。这是根据需要写的SQL语句
private string strsql = "select name from bs";
public DataSet sqlexecutesql4ds(string strsql,string str1)
{
using (SqlConnection mycn = new SqlConnection(str1))
{
mycn.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(strsql, mycn))
{
DataSet ds = new DataSet("ds");
sda.Fill(ds);
return ds;
}
}
}
//然后调用sqlexecutesql4ds函数
DataSet ds = new DataSet();
ds = sqlexecutesql4ds(strsql,str1);
string values = " ";
for(int i = 0;i < ds.Tables[0].Rows.Count; i++)
{
values = values + ds.Tables[0].Rows[i][0].ToString() + "<br>";
}
ds.Dispose();
注意以上代码修改下数据库连接。修改下你查询的SQL语句就直接可以用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我觉得是这样的SqlConnection con=new SqlConnection("连接字符串");
SqlDataAdapter sda=new SqlDataAdapter("SQL语句",con);
DataSet ds=new DataSet();
sda.Fill(ds);
string val=ds.Table[0].Rows[第几行]["列名"].ToString();
SqlDataAdapter sda=new SqlDataAdapter("SQL语句",con);
DataSet ds=new DataSet();
sda.Fill(ds);
string val=ds.Table[0].Rows[第几行]["列名"].ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询