asp.net (C#) 连接sql数据库后如何进行操作

这是我的连接代码不知道对不对?这是在cs文件中写的代码usingSystem.Data.SqlClient;publicpartialclass_Default:Syst... 这是我的连接代码不知道对不对?这是在cs文件中写的代码
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConnection = "user id=binbinceshi;password=binbinceshi;";
strConnection += "initial catalog=Northwind;Server=127.0.0.1;";
strConnection += "Connect Timeout=30";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand com = new SqlCommand("selcet * from ceshi", objConnection);

}
}
到这里后再应该怎么操作才可以显示出里面的数据来,然后进行操作呢,希望各位高手能够指点,谢谢,如果您能帮我,可以与我联系,我QQ:350532994
我现在对于数据库已经晕了头了,所以大家在告诉小弟的时候,希望能写清楚点,谢谢各位兄弟姐妹了,谢谢
展开
 我来答
百度网友6f75ac8fa
2007-06-12 · TA获得超过2513个赞
知道大有可为答主
回答量:1.3万
采纳率:0%
帮助的人:4019万
展开全部
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace M56kc_Forum
{
/// <summary>
/// DBOperate 的摘要说明。
/// </summary>
public class DBOperate
{
public DBOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

//输入SELECT语句,获得返回数据集
public static DataTable SelectFormForum(string sql,int start,int size,string tablename)
{
try
{
SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
DataSet returns = new DataSet();
sqlAdapter1.Fill(returns,start,size,tablename);
return returns.Tables[0];
}
catch (Exception ex)
{
throw (ex);
}
}

public static DataTable SelectFormForum(string sql)
{
try
{
SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
DataSet returns = new DataSet();
sqlAdapter1.Fill(returns);
return returns.Tables[0];
}
catch (Exception ex)
{
throw (ex);
}
}

//返回一条记录
public static DataRow GetDataRow(string sql,string TableName)
{
try
{
SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
DataSet product = new DataSet();
sqlAdapter1.Fill(product, TableName);
return product.Tables[0].Rows[0];
}
catch (Exception ex)
{
throw (ex);
}
}

//执行SQL语句,如UPDATE,INSERT等
public static void ExcSQL(string s)
{
SqlConnection sqlConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
try
{
SqlCommand sqlCmd =new SqlCommand(s,sqlConnection);
sqlConnection.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlConnection.Close();
}
}

//获取用户短消息条数
public static int ShortMsgCount(string UserName)
{
try
{
string s="SELECT COUNT(*) AS cun FROM ShortMessage WHERE (systemmsg = 0) AND (Readed = 0) AND (UserName = '"+UserName+"')";
DataRow dr=GetDataRow(s,"ShortMessage");
return System.Convert.ToInt32(dr["cun"].ToString());
}
catch
{
return 0;
}
}

//计算总共有多少条问题
public static int CalculateQRecord(int BID,int SID,int solved)
{
try
{
string s="SELECT COUNT(*) AS co FROM Questions WHERE (BigClass_ID = "+BID+") AND (SmallClass_ID = "+SID+") AND (kind > 0) AND (kind < 5) AND (solved = "+solved+")";
DataRow dr=GetDataRow(s,"Questions");
return System.Convert.ToInt32(dr["co"].ToString());
}
catch
{
return 0;
}
}

//计算总共有多少条回复
public static int CalculateARecord(int QID)
{
try
{
string s="SELECT COUNT(*) AS co FROM Answers WHERE (Question_ID = "+QID+")";
DataRow dr=GetDataRow(s,"Answers");
return System.Convert.ToInt32(dr["co"].ToString());
}
catch
{
return 0;
}
}

}
}
静静宝贝E7648
2007-06-12 · 超过17用户采纳过TA的回答
知道答主
回答量:60
采纳率:100%
帮助的人:48.3万
展开全部
public DataSet GetConDB(string sql) { SqlConnection conn = new SqlConnection(); DataSet ds = new DataSet(); try { string strConnection = "user id=binbinceshi;password=binbinceshi;"; strConnection = "initial catalog=Northwind;Server=127.0.0.1;"; strConnection = "Connect Timeout=30"; conn = new SqlConnection(strConnection); conn.Open(); SqlDataAdapter sda = new SqlDataAdapter(sql, conn); sda.Fill(ds); return ds; } catch (Exception ex) { throw (ex); } finally { conn.Close(); } }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
阿贺dw
2007-06-25 · TA获得超过612个赞
知道小有建树答主
回答量:1584
采纳率:99%
帮助的人:317万
展开全部
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection strConnection = new SqlConnection("uid=binbinceshi;pwd=binbinceshi;server=.;database=数据库名称");
DataSet ds = new DataSet();
SqlDataAdapter dap = new SqlDataAdapter("select * from 表名",strConnection);
dap.fill(ds);
this.DataGrid1.datasource = ds.table["表名"];
this.dataGrid1.databinder();

}
试试吧!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式