当我在C#语言中进行3层项目时,我无法尝试在读取器关闭时调用Read错误.我想要做的是通过将两个表连接在一起并在下拉列表中显示来检索地址数据列.这是我的数据访问层:
public List<Distribution> getDistributionAll()
{
List<Distribution> distributionAll = new List<Distribution>();
string address;
SqlDataReader dr = FoodBankDB.executeReader("SELECT b.addressLineOne FROM dbo.Beneficiaries b INNER JOIN dbo.Distributions d ON d.beneficiary = b.id");
while (dr.Read())
{
address = dr["addressLineOne"].ToString();
distributionAll.Add(new Distribution(address));
}
return distributionAll;
}
Run Code Online (Sandbox Code Playgroud)
这是我的FoodBankDB类:
public class FoodBankDB
{
public static string connectionString = Properties.Settings.Default.connectionString;
public static SqlDataReader executeReader(string query)
{
SqlDataReader result = null;
System.Diagnostics.Debug.WriteLine("FoodBankDB executeReader: " + query);
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
result = …Run Code Online (Sandbox Code Playgroud) 我正在尝试从PowerShell脚本中读取SQL表中的数据.我可以在reader对象中看到数据,但是在使用While (readerobject.read()){}它时,它不会进入循环内部.
Powershell:
$cmd = $sqlConn.CreateCommand()
$cmd.CommandText ="SELECT * from user"
$movedUserDetails = $cmd.ExecuteReader()
while ($movedUserDetails.Read())
{
"[0] : " + $movedUserDetails.GetValue(0)
}
$movedUserDetails.Close()
Run Code Online (Sandbox Code Playgroud) 当我尝试对从数据库收到的数据执行 dataReader.Read 时,会发生上述错误。我知道那里有两行,所以它不是,因为实际上不存在数据。
是否是 CommandBehavior.CloseConnection 导致了问题?有人告诉我你必须在 ExecuteReader 之后立即执行此操作?它是否正确?
try
{
_connection.Open();
using (_connection)
{
SqlCommand command = new SqlCommand("SELECT * FROM Structure", _connection);
SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (dataReader == null) return null;
var newData = new List<Structure>();
while (dataReader.Read())
{
var entity = new Structure
{
Id = (int)dataReader["StructureID"],
Path = (string)dataReader["Path"],
PathLevel = (string)dataReader["PathLevel"],
Description = (string)dataReader["Description"]
};
newData.Add(entity);
}
dataReader.Close();
return newData;
}
}
catch (SqlException ex)
{
AddError(new ErrorModel("An SqlException error has occured whilst trying to …Run Code Online (Sandbox Code Playgroud) 我试图使用C#运行查询,我遇到以下问题
ConnectionString中未指定OLE DB提供程序.一个例子是'Provider = SQLOLEDB;
我的代码
string strConString = System.Configuration.ConfigurationManager.ConnectionStrings["WorkflowConnStr"].ConnectionString.ToString();
string sqlstr = "select * from table"
OleDbConnection myConnection = new OleDbConnection(strConString);
try
{myConnection.Open();}
catch (Exception err)
{ System.Diagnostics.Debug.WriteLine(err.Message); }
OleDbCommand myCommand = new OleDbCommand(sqlstr, myConnection);
OleDbDataReader reader = myCommand.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)
web.config中
<add name="WorkflowConnStr" connectionString="Data Source=Server;Initial Catalog=DBName;user id=usr;password=password" providerName="System.Data.OleDb.OleDbConnection"/>
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
几天来一直坚持这个问题,请帮忙。我尝试了很多解决方案,例如在网络配置中增加连接超时:下面是我的错误代码。
Server Error in '/' Application.
The wait operation timed out
Run Code Online (Sandbox Code Playgroud)
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
Exception Details: System.ComponentModel.Win32Exception: The wait operation timed out
Run Code Online (Sandbox Code Playgroud)
来源错误:
Line 595: SqlCommand cmd = new SqlCommand(str, con);
Line 596: con.Open();
Line 597: rd = cmd.ExecuteReader();
Line 598: //cs.CloseConnection();
Line 599: return rd;
Source File: c:\inetpub\vhosts\xyz.com\httpdocs\App_Code\admin.cs Line: 597
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
[Win32Exception (0x80004005): The wait operation timed out]
[SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.]
System.Data.SqlClient.SqlConnection.OnError(SqlException …Run Code Online (Sandbox Code Playgroud) 我的问题始于Entity Framework查询执行速度非常慢(约 2 分钟)。所以我开始调查
同时,问题似乎也在标准中SqlConnection
我有一个非常简单的查询
SELECT 1 AS [C1], [Extent1].[OldReleaseID] AS [OldReleaseID], [Extent1].[ProductName] AS [ProductName], [Extent1].[Price] AS [Price], [Extent1].[DiscountAmount] AS [DiscountAmount], [Extent1].[DiscountRate] AS [DiscountRate], [Extent1].[AbsorbVat] AS [AbsorbVat], [Extent1].[SerialCode] AS [SerialCode], [Extent1].[BrandName] AS [BrandName] FROM [dbo].[LocalSaleProductExts]() AS [Extent1]
Run Code Online (Sandbox Code Playgroud)
当我运行它时,SSMS它会在 0-1 秒内执行并返回大约 30k 行
同样的精确查询在 .net 中运行ExecuteReader大约 100 秒!
在线研究基本上指出了 2 个解决方案:ARITHABORT 和参数嗅探,所以为了好玩,我将所有这些添加到 SSMS 中
DBCC FREESESSIONCACHE
DBCC FREEPROCCACHE
SET ARITHABORT OFF
Run Code Online (Sandbox Code Playgroud)
还有0-1秒。
在我的代码中我添加了SET ARITHABORT ON. 这是我的简单代码
Using sc = New SqlClient.SqlConnection("data …Run Code Online (Sandbox Code Playgroud) 当QuestionnaireID匹配我的查询时,我正在使用ExecuteReader返回QuestionText的值.(参见数据库设计)
但是我的问题是,当我运行项目时,我的ExecuteReader只返回输入的最后一个值.例如,如果我要在问卷ID为1下创建3个问题(A,B,C).ExecuteReader将仅返回问题C.
如何返回QuestionText列中具有相同问卷ID的所有值?
数据库设计

****.**CS
public string GetQuestionName(int QuestionnaireID)
{
string returnvalue = string.Empty;
SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
myCommand.Parameters[0].Value = QuestionnaireID;
_productConn.Open();
SqlDataReader test = myCommand.ExecuteReader();
while (test.Read())
{
returnvalue = test.GetString(0);
}
_productConn.Close();
return returnvalue;
}
Run Code Online (Sandbox Code Playgroud)
存储过程
USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hbo].[GetQuestion] Script Date: 11/12/2011 13:12:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[GetQuestion]
(
@QUEST_ID int
)
AS
/*SET NOCOUNT ON;*/
SELECT QuestionText FROM …Run Code Online (Sandbox Code Playgroud) 我有这个代码,它只返回第一个字符串[0]和其他人的错误,说索引超出数组,这意味着只有1行被拉,但我不知道为什么!
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = new MySqlCommand("SELECT email_address FROM account_info", connection);
MySqlDataReader reader;
try
{
connection.Open();
reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
textBox1.Text = reader[0].ToString();
textBox2.Text = reader[0].ToString();
textBox3.Text = reader[0].ToString();
}
reader.Close();
}
Run Code Online (Sandbox Code Playgroud) 大家好我有这个代码:
SqlCommand scom = new SqlCommand(
"INSERT INTO klient(name,surname)
values(@kname,@ksurname)",
conn);
scom.Parameters.AddWithValue("@kname", kname.Text);
scom.Parameters.AddWithValue("@ksurname", ksurname.Text);
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM klient", spojeni);
SDA.Fill(dt);
conn.Close();
Run Code Online (Sandbox Code Playgroud)
它应该插入来自文本框的数据:kname,ksurname,但它关闭表单而不在MS SQL表中显示它们klient
I'm getting a syntax error from my SQL command shown in the code below. The error is happening on the ExecuteReader line, saying
Incorrect syntax near the keyword 'JOIN'.'
I have no idea why it's throwing a syntax error, the command works perfectly fine in SQL Server. Here's the code:
private void button1_Click(object sender, EventArgs e)
{
SqlCommand sqlcomViewSmashRoster;
SqlDataReader dataReader;
String strSql, strOutput ="";
strSql = "SELECT Roster.CharacterName, Roster.TotalTournamentsParticiaped, Roster.TotalWins, Roster.TotalLosses, Roster.Championships, Roster.InjuriesCaused, Roster.Injured" +
"FROM Roster" +
"INNER …Run Code Online (Sandbox Code Playgroud) protected void Button3_Click(object sender, EventArgs e)
{
cn.Open();
SqlCommand cmd = new SqlCommand("select top '"+Label4.Text+"' * from qb_vb where marks=1");
SqlDataReader dr1 = cmd.ExecuteReader();
if (dr1.Read())
{
Label8.Text = dr1["quest"].ToString();
Label9.Text = dr1["ans1"].ToString();
}
cn.Close();
Run Code Online (Sandbox Code Playgroud)
}
我在Wigley和Wheelwright的"Microsoft .NET Compact Framework"中修改了下面的代码:
SqlCeConnection cn = new SqlCeConnection(@"Data Source=\My Documents\Traffic.sdf");
String sSQL= "SELECT CarID, Reg, Location FROM Cars");
SqlCeCommand cmdSelect = new SqlCeCommand(sSQL, cn);
cmdSelect.CommandType = CommandType.Text;
SqlCeDataReader dtr = cmdSelect.ExecuteReader(CommandBehavior.Default); // It's "cmd." instead of "cmdSelect." in the book, but that doesn't compile
while (dtr.Read())
{
ListViewItem item = new ListViewItem(dtr.GetInt32(0).ToString());
item.SubItems.Add(dtr.GetString(1));
item.SubItems.Add(dtr.GetString(2));
listViewCars.Items.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
...所以我的代码是这样的:
private String getDataAsXMLFromTable(String tableName, String siteNum)
{
String strXML;
StringBuilder sbXML = new StringBuilder();
String lineId;
String refNum;
String upcCode;
String desc; …Run Code Online (Sandbox Code Playgroud) compact-framework executereader windows-ce sql-server-ce sqlcedatareader
executereader ×12
c# ×8
asp.net ×5
sql ×4
.net ×2
sql-server ×2
3-tier ×1
ado.net ×1
datareader ×1
insert ×1
mysql ×1
powershell ×1
return ×1
sqlcommand ×1
ssms ×1
syntax-error ×1
windows-ce ×1