页面加载中的SQL连接 [英] SQL Connection on pageload

查看:59
本文介绍了页面加载中的SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在page_load上初始化sql连接

-从解决方案中编辑添加的代码-
背后的代码:

how to initialize sql conncetion on page_load

-- Edit Added Code from Solution --
Code Behind :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
SqlConnection con = new SqlConnection(@"Data Source=SERVER NAME;Initial Catalog=DB NAME;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from TABLENAME where TicketID=" + TextBox1.Text + "",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}
}



新密码



New Code

protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TicketId", TextBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}





create proc sp_select
@TicketId bigint
as begin
select * from TABLENAME where TicketId=@TicketId





protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TicketId", Convert.ToInt64(TextBox1.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
}

推荐答案

如果要在page_Load上获取数据,则将该代码放入page_load中,而不是Button_click中
If you want to get data on page_Load, then put that code into page_load, not in Button_click


我想你想做下面的事情.

I guess you want to do something like below.

SqlConnection con = null;
protected void Page_Load(object sender, EventArgs e)
{
  con = new SqlConnection(@"Data Source=SERVER NAME;Initial Catalog=DB  NAME;Integrated Security=True");
}


hai da请查收此编码da .....



hai da kindly chk this coding da.....



using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    
    protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=10.242.17.143;Initial Catalog='CCloud_Metlife_21JanQA'; uid='Cclouddev';pwd=password-12;");
        con.Open();
        SqlCommand cmd = new SqlCommand("getexception", con);
        cmd.CommandType=CommandType.StoredProcedure;
        SqlParameter sqlParam = new SqlParameter();
        sqlParam.ParameterName = "@TicketID";
        sqlParam.DbType = DbType.Int64;
        sqlParam.Value=TextBox1.Text;
        sqlParam.Direction = ParameterDirection.Input;
        cmd.Parameters.Add(sqlParam);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
        con.Close();
    }
}


这篇关于页面加载中的SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆