发布网友 发布时间:2022-04-25 12:18
共3个回答
懂视网 时间:2022-04-10 08:56
{
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=kd7123_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代码实例
标签:nbsp lag man bool switch ati host seq cas
热心网友 时间:2022-04-10 06:04
连接数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
public static void main(String[] args) {
try {
// 1.注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("注册驱动成功");
String url = ""jdbc:microsoft.sqlserver://127.0.0.1:1433;DatabaseName="; //数据库IP,端口,数据库名
String username = "";
String password = "";
// 加载驱动并获取数据库连接对象
Connection connection = DriverManager.getConnection(url, username,
password);
System.out.println("连接数据库成功!");
// 创建Statement对象
Statement stmt = connection.createStatement();
// SQL语句
String sql = "insert into student(id,name,age,email,address) values(1,'tom',23,'dfg@163.com','England') ";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("插入成功!");
}
// 释放资源
stmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
预编译的数据库处理
public class ConnectionManager{
public static final String DRIVER_CLASS ="";
public static final String URL = "";
public static final String USERNAME = "";
public static final String PASSWORD = "";
//获取连接对象
public static Connection getConnection(){
Connection conn = null;
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
return conn;
}
public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
try{
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
}
}
public class DBOperator{
public int executeUpdate(String sql,Object[] params){
int rows = 0;
Connection connn = ConnectionManager.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1,params[i]);//下标从1开始
}
}
rows = pstmt.executeUpdate();
ConnectionManager.closeAll(null,pstmt,conn);
return rows;
}
public ResultSet executeQuery(String sql,Object[] params){
Connetion conn = ConnectionManager.getConnection();
if(params!=null){
for(int i= 0;i<params.length;i++){
pstmt.setObject(i+1,params(i));
}
}
rs=pstmt.executeQuery();
ConnectionManager.closeAll(rs,pstmt,conn);
return rs;
}
}
/**
*statement连接数据库
*/
public class TestOperatorGrade{
private static void updateGrade(int id,String name,String description){
Class.forName();//注册驱动
Connection conn = DirverManager.getConnection("","","");
Statement stmt = conn.createStament();
String sql = "UPDATE grades set name = '"+name+"',description='"+description+"'WHere id="+id;
int rows = stmt.executeUpdate(sql);
}
}
这是java 程序的代码
热心网友 时间:2022-04-10 07:22
//插入
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL鏁版嵁搴揿紩鎿�
String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest";//鏁版嵁婧惵�
String user="sa";
String pwd="sql";
try {
// 动态导入数据库的驱动
Class.forName(JDriver);
// 获取数据库链接
conn = DriverManager.getConnection(connectDB,user,pwd);
// 创造SQL语句
String sql = "INSERT INTO JDBCTestTable ( user_name, user_password ) VALUES ( 'balabala', '123456' )";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("插入数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}
//查询
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sql";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="SELECT * FROM JDBCTestTable";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
ResultSet rs=stmt.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString("user_name")+":"+rs.getString(2));
//密码字段的编号从1开始,密码排第二位
}
System.out.println("查询数据成功");
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//删除
public static void main(String[] args){
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest";
String user="sa";
String password="sqlyanghai";
try{
Class.forName(JDriver);// 动态导入数据库的驱动
Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接
String query="DELETE FROM JDBCTestTable WHERE user_name='Eric'";// 创造SQL语句
Statement stmt=conn.createStatement();// 执行SQL语句
stmt.executeUpdate(query);
System.out.println("删除数据成功");
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
//修改
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 动态导入数据库的驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获取数据库链接
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest",
"sa", "sql");
// 创造SQL语句
String sql = "UPDATE JDBCTestTable SET user_password = '456' WHERE user_name = 'Eric'";
// 执行SQL语句
stmt = conn.createStatement();
stmt.executeUpdate(sql);
System.out.println("更新数据成功");
} catch (Exception e) {
e.printStackTrace();
stmt.close();
conn.close();
}
}