连接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() + "";
break;
case ("DoubleClick"):
this.Message.Text += "Double clicked GridView row at index "
+ _selectedIndex.ToString() + "";
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);
}

}
}

转载于:https://www.cnblogs.com/yaoxiaozhong/p/7411473.html

你可能感兴趣的:(连接Sqlserver2008代码实例)