连接Sqlserver2008代码实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接Sqlserver2008代码实例相关的知识,希望对你有一定的参考价值。
{
public partial class _Default : System.Web.UI.Page
{
private SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
HttpContext context1 = HttpContext.Current;
HttpContext context2 = System.Runtime.Remoting.Messaging.CallContext.HostContext as HttpContext;
bool isEqual = object.ReferenceEquals(context1, context2);
Response.Write("两个对象值"+ isEqual);
//添加行数据
try
{
DataTable dt = SelectZhiboTop(" z_flag=1 order by id desc");
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
catch
{
Response.Write("绑定数据库错误!");
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;
// Get the selected index and the command name
int _selectedIndex = int.Parse(e.CommandArgument.ToString());
string _commandName = e.CommandName;
switch (_commandName)
{
case ("SingleClick"):
_gridView.SelectedIndex = _selectedIndex;
this.Message.Text += "Single clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
case ("DoubleClick"):
this.Message.Text += "Double clicked GridView row at index "
+ _selectedIndex.ToString() + "<br />";
break;
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Get the javascript which is assigned to this LinkButton
string _jsSingle =
ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// To prevent the first click from posting back immediately
// (therefore giving the user a chance to double click)
// pause the postbackfor 300 milliseconds by
// wrapping the postback command in a setTimeout
_jsSingle = _jsSingle.Insert(11, "setTimeout(\"");
_jsSingle += "\", 300)";
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[1].Controls[0];
// Get the javascript which is assigned to this LinkButton
string _jsDouble =
ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this javascript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
//查询所有直播
public DataTable SelectZhiboTop(string sql)
{
string sql1 = "select top 2 id,z_title from R_Zhibo ";
if (sql.Trim() != "")
{
sql1 += " where " + sql;
}
DataTable dt = ExecuteDataSet(sql1); // ds.Tables[0];
return dt;
}
public DataTable ExecuteDataSet(string sql)
{
DataSet ds = null;
SqlDataAdapter da = null;
da = new SqlDataAdapter();
ds = new DataSet();
DataTable dt = null;
dt = new DataTable();
//SqlCommand cmd = new SqlCommand();
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=yaoxiaozhong;Initial Catalog=LZH_JY_DB;uid=sa;pwd=kd789123_qwe; Max Pool Size=150; Connect Timeout=500;";
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
//cmd.CommandType = BaseParser;
da.SelectCommand = cmd;
da.Fill(dt);
}
catch
{ throw; }
return dt;
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation
(r.UniqueID + "$ctl01");
}
}
//base.Render(writer);
}
}
}
以上是关于连接Sqlserver2008代码实例的主要内容,如果未能解决你的问题,请参考以下文章
Linux环境PHP5.5以上连接SqlServer2008
如何将java程序里的一段连接mysql的语句转为连接sqlserver语句。连接的是sqlser
c# vs2010连接SQLServer2008时出现“未将对象引用设置到对象的实例。”