我正在以这种方式执行命令:
var Command = new SqlCommand(cmdText, Connection, tr);
Command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
在命令中有一个错误,但.NET不会抛出任何错误消息.我怎么知道命令没有正确执行,以及如何获得异常?
默认的CommandTimeout值为30秒.您可以通过执行以下操作手动更改命令对象实例上的值
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandTimeout = 60
Run Code Online (Sandbox Code Playgroud)
有没有办法指定不同的默认值,以便所有新命令对象在创建时在解决方案中自动具有此值?
当使用调用SqlCommand.ExecuteReader()方法时,ReSharper告诉我,当我之后使用SqlDataReader对象时,我有一个可能的NullReference异常.
所以使用以下代码:
using (SqlConnection connection = GetConnection())
{
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = ; //snip
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//snip
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该while (reader.Read())行加下划线.
我的问题是读者对象何时会为空?我从来没有遇到它,文档没有提到它可能.我应该检查它是否为空或是否可以安全忽略?
为什么ReSharper认为它可能为null,例如它让我使用SqlCommand而不建议检查null?我猜是ExecuteReader方法有一个属性.
我正在使用这些代码行来检查记录是否存在.
SqlCommand check_User_Name = new SqlCommand("SELECT * FROM Table WHERE ([user] = '" + txtBox_UserName.Text + "') ", conn);
int UserExist = (int)check_User_Name.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
我想要做:
if (UserExist > 0)
// Update record
else
// Insert record
Run Code Online (Sandbox Code Playgroud) 我已经看到这在代码中显示了几个地方,从来没有解释,只是上面的一个神秘的评论(声明和执行包含在上下文的想法.它只是运行SqlCommand的标准过程):
//SqlCommand cmd = new SqlCommand();
//cmd.ExecuteReader();
//Read off the results
//Cancel the command. This improves query time.
cmd.Cancel ();
Run Code Online (Sandbox Code Playgroud)
基本上,在完成一个查询后,它会返回并取消它,声称会有一些性能提升.我想你可能会获得一些内存,并释放XmlReader,但通常情况下它会超出范围.
我之前从未打扰过它,但它最终出现在我正在审查的一些代码中.在代码中运行它后取消SqlCommand实际上是以某种方式加速它,还是这只是一些奇怪的程序员迷信?
我试图让标题尽可能具体.基本上我在后台工作线程中运行的是现在的一些代码,如下所示:
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(Results);
conn.Close();
sda.Dispose();
Run Code Online (Sandbox Code Playgroud)
其中query是表示大量耗时查询的字符串,conn是连接对象.
我现在的问题是我需要一个停止按钮.我已经意识到杀死背景工作者将毫无价值,因为我仍然希望在取消查询后保留结果.此外,在查询之后,它将无法检查已取消的状态.
到目前为止我想出的是:
我一直试图概念化如何有效地处理这个问题,而不会影响太大的性能.
我的想法是使用SqlDataReader一次从查询片段中读取数据,以便我有一个"循环"来检查我可以通过按钮从GUI设置的标志.问题是据我所知,我不能使用数据表的Load()方法,仍然能够取消sqlcommand.如果我错了请告诉我,因为这会使取消稍微容易一些.
根据我发现的内容,我实现了,如果我执行类似下面的操作(伪代码),我可能只能取消sqlcommand mid-query:
while(reader.Read())
{
//check flag status
//if it is set to 'kill' fire off the kill thread
//otherwise populate the datatable with what was read
}
Run Code Online (Sandbox Code Playgroud)
然而,在我看来,这将是非常无效的,并且可能代价高昂.这是杀死正在进行的绝对需要在数据表中的sqlcommand的唯一方法吗?任何帮助,将不胜感激!
我在Windows XP上使用Java,并希望能够将命令发送到另一个程序,如telnet.我不想简单地执行另一个程序.我想执行它,然后在它运行后发送一系列命令.这是我想要做的代码,但它不起作用:(如果取消注释并将命令更改为"cmd",它将按预期工作.请帮助.)这是一个简化的示例.在生产中会发送更多的命令,所以请不要建议调用"telnet localhost".
try
{
Runtime rt = Runtime.getRuntime();
String command = "telnet";
//command = "cmd";
Process pr = rt.exec(command);
BufferedReader processOutput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
String commandToSend = "open localhost\n";
//commandToSend = "dir\n" + "exit\n";
processInput.write(commandToSend);
processInput.flush();
int lineCounter = 0;
while(true)
{
String line = processOutput.readLine();
if(line == null) break;
System.out.println(++lineCounter + ": " + line);
}
processInput.close();
processOutput.close();
pr.waitFor();
}
catch(Exception x)
{
x.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我想知道,这个SqlCommand构造函数重载的原因是什么:
public SqlCommand(
string cmdText,
SqlConnection connection,
SqlTransaction transaction
)
Run Code Online (Sandbox Code Playgroud)
?
当我需要创建一个使用作为参数提供的事务来完成其位的内部方法时,我总是觉得仅传递SqlTransaction给该方法就足够了,因为显然,连接将是tran.Connection.
这种过载是否同样适用?只通过cmdText而且还不够transaction吗?
实际上是否可以SqlCommand针对连接执行,提供SqlTransaction针对不同 的打开SqlConnection?这会导致什么结果?
每个人我都是学生,不熟悉.NET,特别是MVC3开发,但对于我的一个项目,我要研究它,因此经历学习阶段我面临的问题和混乱是关于数据库连接,我看到了什么.关于从数据库中检索记录是这样的:
//Method One:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM table";
var cmd = new SqlCommand(cmdString, conn);
var mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter.Fill(myDataSet, "design");
// making a new SqlCommand object with stringQuery and SqlConnection object THEN a new SqlDataAdapter object with SqlCommand object and THEN filling up the table with the resulting dataset.
Run Code Online (Sandbox Code Playgroud)
但是当我查看MSDN Library时,我发现SqlDataAdapter提供了一个构造函数SqlDataAdapter(String,String),它直接接受一个SelectCommand和一个连接字符串来启动,从而跳过SqlCommand之间的角色,如下所示:
//Method Two:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM …Run Code Online (Sandbox Code Playgroud) 以下面的例子为例......
Using cn As New SqlConnection(ConnectionString)
Try
Dim cmd As SqlCommand = New SqlCommand
With cmd
.Connection = cn
.Connection.Open()
.CommandText = "dbo.GetCustomerByID"
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@CustomerID", SqlDbType.Int, 4)
.Parameters("@CustomerID").Value = CustomerID
End With
da = New SqlDataAdapter(cmd)
da.Fill(ds, "Customer")
Catch ex As Exception
End Try
End Using
Run Code Online (Sandbox Code Playgroud)
根据我今天的研究,听起来好像这基本上还可以,但是SqlCommand没有被处理掉.
问题 - >以下哪个例子是处理此问题的最佳方法?
示例2 - 手动处理
Using cn As New SqlConnection(ConnectionString)
Try
Dim cmd As SqlCommand = New SqlCommand
With cmd
.Connection = cn
.Connection.Open()
.CommandText = "dbo.GetCustomerByID"
.CommandType = CommandType.StoredProcedure …Run Code Online (Sandbox Code Playgroud) sqlcommand ×10
c# ×5
.net ×3
ado.net ×2
sql ×2
vb.net ×2
command-line ×1
dispose ×1
java ×1
process ×1
resharper ×1
send ×1
sql-server ×1
sqlclient ×1
winforms ×1