连接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);
}

}
}

时间: 2024-10-24 14:40:18

连接Sqlserver2008代码实例的相关文章

Redis教程(十五):C语言连接操作代码实例

转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/143.html 在之前的博客中已经非常详细的介绍了Redis的各种操作命令.运行机制和服务器初始化参数配置.本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例.然而需要说明的是,由于Redis官方并未提供基于C接口的Windows平台客户端,因此下面的示例仅可运行于Linux/Unix平台.但是对于使用其它编程语言的

appium如何连接模拟器代码实例

from appium import webdriver def connect(self): self.desired_caps = {} self.desired_caps['platformName'] = 'Android' self.desired_caps['platfornVersion'] = '4.4.4' self.desired_caps['deviceName'] = 'Samsung Galaxy S4-4.4.4' self.desired_caps['appPack

Python连接MySQL的实例代码

Python连接MySQL的实例代码 MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME%/Lib/site-packages目录中,python会自动找到此包. MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0. 其他: 1. 平台及版本 linux 内核2.6,gcc 3.4.4,glibc 2

php扩展mssql.so连接sqlserver2008

1.安装配置freetds  wget http://mirrors.xmu.edu.cn/ubuntu/archive/pool/main/f/freetds/freetds_0.82.orig.tar.gz tar zxf freetds_0.82.orig.tar.gz cd freetds_0.82 ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 -–enable-msdblib -–enable-dbmfix -–wi

flex3+struts 1.3+spring+ibatis 2.x整合代码实例

原创整理不易,转载请注明出处:flex3+struts 1.3+spring+ibatis 2.x整合代码实例 代码下载地址:http://www.zuidaima.com/share/1778657261997056.htm 经过两天工作闲余时间的奋战,终于flex3+struts 1.3+spring +ibatis 2.x 整合成功,下面介绍下详细的步骤和核心代码: IDE:myeclipse (当然:前提是FLEX+java整合成功的情况下,关于flex+java整合的文章就比较多,go

将表单元素序列为对象代码实例

将表单元素序列为对象代码实例:有时候将表单元素序列化一个对象然后再进行操作可能会更加便利,下面就是一段这样的代码能够实现此功能.代码如下: function serializeObject(form){ var o={}; $.each(form.serializeArray(),function(index){ if(o[this['name']]){ o[this['name']]=o[this['name']]+","+this['value']; } else{ o[this[

Java连接SqlServer2008数据库

Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下载 完成后,是个exe文件,点击运行,会提示你选择解压目录. 解压完成后,进入 <你解压到得目录>\sqljdbc_3.0\chs,里边有两个我们需要的东东 一个是:sqljdbc.jar,另外一个是sqljdbc4.jar 这里使用sqljdbc4.jar 首先配置sa身份验证: 由于安装sql

点击文本框弹出可供选择的checkbox复选框代码实例

点击文本框弹出可供选择的checkbox复选框代码实例:本章节分享一段代码实例,它能够点击文本框的时候,能够弹出下拉的checkbox复选框,选中复选框就能够将值写入文本框中,可能在实际应用中的效果没有这么直白简单,不过可以作为一个例子演示,以便于学习者理解和扩展.代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author&qu

ASP连接sql server实例解析

1.首先确定自己的iis没有问题 2.其次确定自己sqlserver没有问题 然后在iis的文件夹wwwroot里,建立一个文件 名为testSqlServer.asp,编写代码例如以下就可以 <% 'ole db连接 set cnn1 = Server.CreateObject("ADODB.Connection") '连接串需要注意sqlserver的实例名,是否是默认,非默认必需要写出来 cnn1.Open "provider=sqloledb;data sour