我正在尝试使用可数据对象来加载 sql 查询的结果。有些查询有效,有些则无效。我已经对数据库进行了跟踪,我可以看到正确的 sql 通过,但我的 aspx 页面运行不正常。当我使用 sharepoint 2010 运行页面时,我收到的错误消息非常无用。我怀疑这是 dataTable 不喜欢的某种数据类型(因为我想不出它还能是什么)但我不知道它可能是什么。有人可以帮忙吗?
<%@ page language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System" %>
<script runat="server">
//Create string objects
//Create a data table object
DataTable dataTable = new DataTable();
//On page load get the parameters from the URL and assign them to the objects
protected void page_load (Object s, EventArgs e)
{
}
//Create new class
public class ParamDemo
{
//Create new method to getData
public static DataTable GetData()
{
// create connection and reader variables
SqlConnection conn = null;
SqlDataReader reader = null;
//Create a data table object
DataTable dataTable = new DataTable();
//String cmdString = new String();
try
{
// instantiate and open connection
conn = new SqlConnection("Server=myserver;Database=myDB;User Id=UserId;Password=Password");
conn.Open();
// 1. declare required command object
String cmdString = "";
cmdString = "SELECT TOP 1 DMA FROM [myDb].[dbo].[dev_table]";
SqlCommand cmd = new SqlCommand(
cmdString, conn);
//get data stream
reader = cmd.ExecuteReader();
//Record datastream in datatable
dataTable.Load(reader);
}
finally
{
// close reader
if (reader != null)
{
reader.Close();
}
// close connection
if (conn != null)
{
conn.Close();
}
}
return dataTable;
}
}