c# datagrid_为Asp.net中的新开发人员提供的教程-与(vb和C#)的Sql Server连接并填充Datagrid或Gridview。

c# datagrid

When anyone starts newly development using ASP.net this is very helpful tutorial for connectivity with SQL Server.

当任何人使用ASP.net开始新开发时,这对于与SQL Server的连接都是非常有用的教程。

1.步骤1 [b] (1. STEP 1[b])

在代码隐藏页中导入以下名称空间[] / b]

VB :

VB:

imports System.data

导入System.data

imports System.data.sqlclient

导入System.data.sqlclient

C# :

C# :

using System.data;

使用System.data;

using System.data.sqlclient;

使用System.data.sqlclient;

2.步骤2 (2. STEP 2)

Create Your Connection string

创建您的连接字符串

Example:  Data Source=[Database Server Name];Initial Catalog=[Database Name];User ID=[Database Access UserID];Password=[Database Password]

示例:数据源= [数据库服务器名称];初始目录= [数据库名称];用户ID = [数据库访问用户ID];密码= [数据库 密码]

Declare One Variable and Store above connection string in it Like below

声明一个变量并将其存储在连接字符串上方,如下所示

VB :

VB:

Dim strConnectionString As String = "connection string here"

Dim strConnectionString As String =“此处的连接字符串”

C# :

C# :

string strConnectionString = "connection string here";

string strConnectionString =“此处的连接字符串”;

3.步骤3 (3. STEP 3)

Declare connection string object

声明连接字符串对象

VB:

VB:

Dim cnn As New SqlConnection(strConnectionString)

昏暗的cnn作为新的SqlConnection(strConnectio nString)

C# :

C# :

 SqlConnection cnn = new SqlConnection(strConnectionString);

SqlConnection cnn =新的SqlConnection(strConnectio nString);

4.步骤4 (4. STEP 4)

Declare SQL command object and provide in query from database which you want to display in datagrid and initialize it with connection object

声明SQL命令对象并提供您要在数据库中显示的查询,并使用连接对象对其进行初始化

VB:

VB:

Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM table ", cnn)

Dim cmd As SqlClient.SqlCommand =新的SqlClient.SqlCommand(“ SELE CT * FROM table“,cnn)

C# :

C# :

  SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("SELECT * FROM table ", cnn);

SqlClient.SqlCommand cmd =新的SqlClient.SqlCommand(“ SELE CT * FROM table“,cnn);

5.步骤5 (5. STEP 5)

Set commandType to sqlcommand object.  As we have given Text set it to text ( If we provide storeprocedure name set it to storeprocedure)

将commandType设置为sqlcommand对象 。 如我们给定的Text设置为text(如果我们提供storeprocedure名称,请将其设置为storeprocedure)

VB:

VB:

 cmd.CommandType = CommandType.Text

cmd.CommandType = CommandType.Text

C# :

C# :

  cmd.CommandType = CommandType.Text;

cmd.CommandType = CommandType.Text;

6.步骤6 (6. STEP 6)

Declare DataAdapter object and initialize with command object

声明DataAdapter对象并使用命令对象进行初始化

VB:

VB:

Dim dacmd As New SqlClient.SqlDataAdapter(cmd)

昏暗的dacmd作为新的SqlClient.SqlDataAdapter(c md)

C# :

C# :

  SqlClient.SqlDataAdapter dacmd = new SqlClient.SqlDataAdapter(cmd);

SqlClient.SqlDataAdapter dacmd =新的SqlClient.SqlDataAdapter(c md);

7.步骤7 (7. STEP 7)

Declare Dataset object and fill the data from datadapter

声明数据集对象并填充datadapter中的数据

VB:

VB:

Dim dscmd As New DataSet

Dim dscmd作为新数据集

        dacmd.Fill(dscmd)

dacmd.Fill(dscmd)

C# :

C# :

     DataSet dscmd = new DataSet();

DataSet dscmd = new DataSet();

    dacmd.Fill(dscmd);

dacmd.Fill(dscmd);

8.步骤8 (8. STEP 8)

Finally, Set the dataset to Datagrid

最后, 将数据集设置为Datagrid

VB:

VB:

 Datagrid1.DataSource = dsContract.Tables(0)

Datagrid1.DataSource = dsContract.Tables(0)

                Datagrid1.DataBind()

Datagrid1.DataBind()

C# :

C# :

   Datagrid1.DataSource = dsContract.Tables(0);

Datagrid1.DataSource = dsContract.Tables(0);

    Datagrid1.DataBind();

Datagrid1.DataBind();

VB :
 
    Dim strConnectionString As String = "connetion string here"
    Dim cnn As New SqlConnection(strConnectionString)
    Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM table ", cnn)
    cmd.CommandType = CommandType.Text
    Dim dacmd As New SqlClient.SqlDataAdapter(cmd)
    Dim dscmd As New DataSet
    dacmd.Fill(dscmd)
    Datagrid1.DataSource = dsContract.Tables(0)
    Datagrid1.DataBind()
 
 
C#:
 
 
{ 
    string strConnectionString = "connetion string here"; 
    SqlConnection cnn = new SqlConnection(strConnectionString); 
    SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("SELECT * FROM table ", cnn); 
    cmd.CommandType = CommandType.Text; 
    SqlClient.SqlDataAdapter dacmd = new SqlClient.SqlDataAdapter(cmd); 
    DataSet dscmd = new DataSet(); 
    dacmd.Fill(dscmd); 
    Datagrid1.DataSource = dsContract.Tables(0); 
    Datagrid1.DataBind(); 
} 

翻译自: https://www.experts-exchange.com/articles/218/Tutorial-for-new-developers-in-Asp-net-connectivity-with-Sql-server-for-vb-C-and-fill-the-Datagrid-or-Gridview.html

c# datagrid

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值