我正在尝试使用 SQL 服务器显示来自 Visual Studio 的数据网格
I am trying to display data grid from Visual Studio with SQL Server
我正在尝试使用 Visual Studio 显示来自 SQL 服务器的数据网格,并且我在屏幕截图中显示了此错误。我已经在这里尝试了所有方法,但没有找到任何答案,请查看该屏幕截图。谢谢
[那是有错误的照片]
如果您正在开发 Web 应用程序,请转到下面的 web.config 文件添加,根据您的 SQL 环境设置更改参数。
<configuration>
<connectionStrings>
<add name="PSDatabaseConnectionString" connectionString="Data Source=YourSQLserverName\SQLEXPRESS;Initial Catalog=YourDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
....
然后构建了一个 class 文件,假设我们将其命名为 "ClassSQL",然后构建了一个能够使用 TSQL 从 SQL 服务器获取数据的子方法
public static DataTable RunSQL_DML_FillDataGrid(string TSQL)
{
string connectionString = ConfigurationManager.ConnectionStrings["PSDatabaseConnectionString"].ConnectionString;
SqlDataAdapter dataAdapter;
SqlConnection conn = new SqlConnection(connectionString);
try
{
// Run TSQL on SQL server
dataAdapter = new SqlDataAdapter(TSQL, connectionString);
// MS Term ' Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and return the table.
// MS Term ' Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
return table;
}
catch
{
return null;
}
}
最后从您的主代码中调用 class 方法并将其绑定到网格视图
string TSQL = "select * from TableA";
DataTable dt =ClassSQL.RunSQL_DML_FillDataGrid(TSQL);
GridView1.DataSource = dt;
GridView1.DataBind();
您也可以在其他应用程序类型(控制台、桌面、MVC)上使用它,或者作为直接函数使用,只要您稍微调整一些代码即可。
我正在尝试使用 Visual Studio 显示来自 SQL 服务器的数据网格,并且我在屏幕截图中显示了此错误。我已经在这里尝试了所有方法,但没有找到任何答案,请查看该屏幕截图。谢谢
[那是有错误的照片]
如果您正在开发 Web 应用程序,请转到下面的 web.config 文件添加,根据您的 SQL 环境设置更改参数。
<configuration>
<connectionStrings>
<add name="PSDatabaseConnectionString" connectionString="Data Source=YourSQLserverName\SQLEXPRESS;Initial Catalog=YourDatabaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
....
然后构建了一个 class 文件,假设我们将其命名为 "ClassSQL",然后构建了一个能够使用 TSQL 从 SQL 服务器获取数据的子方法
public static DataTable RunSQL_DML_FillDataGrid(string TSQL)
{
string connectionString = ConfigurationManager.ConnectionStrings["PSDatabaseConnectionString"].ConnectionString;
SqlDataAdapter dataAdapter;
SqlConnection conn = new SqlConnection(connectionString);
try
{
// Run TSQL on SQL server
dataAdapter = new SqlDataAdapter(TSQL, connectionString);
// MS Term ' Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and return the table.
// MS Term ' Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
return table;
}
catch
{
return null;
}
}
最后从您的主代码中调用 class 方法并将其绑定到网格视图
string TSQL = "select * from TableA";
DataTable dt =ClassSQL.RunSQL_DML_FillDataGrid(TSQL);
GridView1.DataSource = dt;
GridView1.DataBind();
您也可以在其他应用程序类型(控制台、桌面、MVC)上使用它,或者作为直接函数使用,只要您稍微调整一些代码即可。