gpt4 book ai didi

c# - 连接忙于 C# 中另一个 hstmt 的结果

转载 作者:行者123 更新时间:2023-11-30 15:34:08 25 4
gpt4 key购买 nike

所以这是我的代码,我真的不知道为什么它在其他程序中有效,这段代码是标准的!!

static void Main(string[] args)
{
using (OdbcConnection DbConnection = new OdbcConnection("DSN=savior"))
{
String query = "***";
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText = query;
try
{
OdbcDataReader DbReader = DbCommand.ExecuteReader();

do
{
int fCount = DbReader.FieldCount;
if (fCount > 0)
{
while (DbReader.Read())
{
using (OdbcConnection DbConnect = new OdbcConnection("DSN=savior"))
{
OdbcCommand DbCom = DbConnect.CreateCommand();
query = System.String.Format("***", DbReader.GetInt16(0));
DbCom.CommandText = query;
try
{
DbCom.ExecuteNonQuery();
}
catch (OdbcException ex)
{
Console.WriteLine("Executing the query2 failed.");
Console.WriteLine("The OdbcCommand returned the following message");
Console.WriteLine(ex.Message);
return;
}
}
}
}
else
{
Console.WriteLine("Query affected row(s)");
}
}
while (DbReader.NextResult());

DbReader.Close();
}
catch (OdbcException ex)
{
Console.WriteLine("Executing the query1 failed.");
Console.WriteLine("The OdbcCommand returned the following message");
Console.WriteLine(ex.Message);
return;
}
}
}

它给了我以下错误,我已经尝试了很多开始和连接的位移,但没有任何 goog 结果。

代码编辑:

static void Main(string[] args)
{
using (OdbcConnection DbConnection = new OdbcConnection("DSN=savior"))
{
DbConnection.Open();
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText = "select ID from Table where ID not in (select IdDA from Seconde)";
try
{
OdbcDataReader DbReader = DbCommand.ExecuteReader();
do
{
int fCount = DbReader.FieldCount;
if (fCount > 0)
{
while (DbReader.Read())
{
using (OdbcConnection DbConnect = new OdbcConnection("DSN=savior"))
{
DbConnect.Open();
OdbcCommand DbCom = DbConnect.CreateCommand();
DbCom.CommandText = System.String.Format("INSERT into Seconde(IdDA,Validee) values({0},'oui')", DbReader.GetInt16(0));
try
{
DbCom.ExecuteNonQuery();
}
catch (OdbcException ex)
{
Console.WriteLine("Executing the query2 failed.");
Console.WriteLine("The OdbcCommand returned the following message");
Console.WriteLine(ex.Message);
return;
}
}
}
}
else
{
Console.WriteLine("Query affected no row(s)");
}
}
while (DbReader.NextResult());
}
catch (OdbcException ex)
{
Console.WriteLine("Executing the query1 failed.");
Console.WriteLine("The OdbcCommand returned the following message");
Console.WriteLine(ex.Message);
return;
}
}
}

最佳答案

这段代码:

OdbcCommand DbCommand1 = DbConnection.CreateCommand();
OdbcCommand DbCommand2 = DbConnection.CreateCommand();

... 在同一连接上创建两个命令,并尝试同时使用它们(使用一个命令插入,同时从另一个命令读取)。我怀疑如果您创建了两个连接,您会更成功。

另外:

  • 不要通过字符串操作在 SQL 语句中包含值。请改用参数化 SQL,否则您很容易受到 SQL injection attacks 的攻击、转换问题和脆弱的代码。
  • 使用using语句自动关闭连接、命令和读取器,无论成功或失败
  • 当您发现异常时,继续前进很少是个好主意。例如,如果您无法打开连接,您仍然会尝试从数据库中读取。那只是自找麻烦。
  • 通常 C# 中的局部变量是驼峰式而不是 Pascal 式。遵循语言的常规约定是一个好主意,以帮助将要阅读您的代码的其他人。

关于c# - 连接忙于 C# 中另一个 hstmt 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com