将连接字符串传递给在设计时创建的数据网格的数据源 [英] pass connection string to datasource of data grid that created at design time

查看:73
本文介绍了将连接字符串传递给在设计时创建的数据网格的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 1-我用asp.net创建了一个网页,并在上面放了一个数据网格,并在设计时配置了我的数据网格.
2-我在SQL Server中定义单个用户,并为每个用户定义用户操作(插入,更新,删除,选择),
3-我如何在运行时创建连接字符串并将该连接字符串传递到数据网格的datasource

解决方案

请看以下文章:
如何在Asp.Net中动态运行时间构建ConnectionString [ ^ ]
在C#运行时期间在App.Config文件中配置连接字符串 [ ^ ]

来自以上链接的示例:

 SqlConnectionStringBuilder ConnBuilder =  SqlConnectionStringBuilder();

ConnBuilder.DataSource =  @" ; //  Sql Server实例的名称
ConnBuilder.InitialCatalog = " ; // 数据库名称
ConnBuilder.UserID = " ;
ConnBuilder.Password = " ;
ConnBuilder.ConnectTimeout =  0 ;
ConnBuilder.IntegratedSecurity =  false ;


// 使用方法
数据表dt;
字符串 SQL = " 使用(SqlConnection Conn =  SqlConnection(ConnBuilder.ConnectionString))
{
    使用(SqlCommand comm =  SqlCommand(SQL,Conn))
     {
         Conn.Open();
         使用(SqlDataAdapter da =  SqlDataAdapter(comm))
         {
             dt =  DataTable("  );
             da.Fill(dt);
         }
     }
}
GridView1.DataSource = dt;
GridView1.DataBind(); 


hi 1-I have created a web page with asp.net and put a data grid on it and have config my data grid in design time.
2-I define individual user in SQL server and define user action for each user(insert, update, delete, select),
3-How could i create connection string at run time and passed this connection string to data grid''s datasource

解决方案

Have a look at the following article:
How to Run Time Dynamically Build ConnectionString in Asp.Net[^]
Configuring a Connection String in the App.Config File During Runtime in C#[^]

Sample from above link:

SqlConnectionStringBuilder ConnBuilder = new SqlConnectionStringBuilder();

ConnBuilder.DataSource = @".\SQLExpress"; // Name of the Sql Server instance
ConnBuilder.InitialCatalog = "TestDB"; // Database Name
ConnBuilder.UserID = "sa";
ConnBuilder.Password = "your password";
ConnBuilder.ConnectTimeout = 0;
ConnBuilder.IntegratedSecurity = false;


//How to use
DataTable dt;
String SQL = "SELECT Name,Description,Size,Color From Product  Order BY Name";
using (SqlConnection Conn = new SqlConnection(ConnBuilder.ConnectionString))
{
    using (SqlCommand comm = new SqlCommand(SQL, Conn))
     {
         Conn.Open();
         using (SqlDataAdapter da = new SqlDataAdapter(comm))
         {
             dt = new DataTable("tbl");
             da.Fill(dt);
         }
     }           
}
GridView1.DataSource = dt;
GridView1.DataBind();


这篇关于将连接字符串传递给在设计时创建的数据网格的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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