C#和SQL Server 2005 [英] C# and SQL Server 2005
本文介绍了C#和SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
以下是我的存储过程和代码。
使用此我想要将数据库中的值加载到我的表单控件数据源属性。
但我无法检索任何值。
我的代码:
_ conn = new SqlConnection(ConnectionString);
var command = new SqlCommand( LoadAppointment,_ conn){CommandType = CommandType.StoredProcedure};
_conn.Open();
var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
我的存储过程:
USE [计划程序]
GO
/ * *****对象:StoredProcedure [dbo]。[LoadAppointment]脚本日期:03/04/2013 14:44:28 ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]。[LoadAppointment]
AS
SELECT * FROM [日程安排]。[dbo]。[tblAppointments]
注意:我不应该使用数据集。
解决方案
您需要提供数据表的架构。
在您的代码中,您只需创建表格 - 但是没有添加任何列。
请通过此链接 - 它将帮助您实现此目的。
DataTable.Load方法
我使用过SqlDataReader而不是使用SqlDataAdapter或DataSet。
DataTable dtTable = new DataTable();
SqlConnection sqlCon = new SqlConnection( @ 服务器= WILLINGTON\SQLEXPRESS;数据库= TESTDB; Trusted_Connection = TRUE;跨度>);
SqlCommand sqlCmd = new SqlCommand( SELECT *来自USERS,sqlCon);
if (sqlCon.State!= ConnectionState.Open)
sqlCon.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
dtTable.Load(dr);
if (sqlCon.State!= ConnectionState.Open)
sqlCon.Close();
Hello,
Below is my Stored Procedure and Code.
Using this i would like to load the values in the database to my Form Controls "Datasource" property.
But i could not be able to retrieve any values.
My Code:
_conn = new SqlConnection(ConnectionString);
var command = new SqlCommand("LoadAppointment", _conn) { CommandType = CommandType.StoredProcedure };
_conn.Open();
var dr = command.ExecuteReader(CommandBehavior.CloseConnection);
var dt = new DataTable();
dt.Load(dr);
My Stored Procedure:
USE [Scheduler]
GO
/****** Object: StoredProcedure [dbo].[LoadAppointment] Script Date: 03/04/2013 14:44:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[LoadAppointment]
AS
SELECT * FROM [Scheduler].[dbo].[tblAppointments]
Note: I am not supposed to use a Dataset.
解决方案
You need to provide the schema for data table.
In you code you have simply created the table - but that does not have any columns added to that.
Please go through this link - It will help you achieving this.
DataTable.Load Method
I have used SqlDataReader instead of using SqlDataAdapter or DataSet.
DataTable dtTable = new DataTable(); SqlConnection sqlCon = new SqlConnection(@"Server=WILLINGTON\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;"); SqlCommand sqlCmd = new SqlCommand("SELECT * FROM USERS", sqlCon); if (sqlCon.State != ConnectionState.Open) sqlCon.Open(); SqlDataReader dr = sqlCmd.ExecuteReader(); dtTable.Load(dr); if (sqlCon.State != ConnectionState.Open) sqlCon.Close();
这篇关于C#和SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文