我在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) 伙计们,
除非我弄错了,否则可以通过为每个参数指定SqlDbType来生成参数化的sql查询.似乎我可以通过提供参数名称和参数值来构造SqlParameter.指定SqlDbType有什么好处?
我在用 Microsoft.SqlServer.Management.Smo.
我的代码:
Server server = new Server(new ServerConnection( new SqlConnection(ConnectionString));
server.ConnectionContext.ExecuteNonQuery(script);
Run Code Online (Sandbox Code Playgroud)
我想像CommandTimeout平常那样设置它SQLCommand
请告诉我如何设置CommandTimeout查询运行Microsoft.SqlServer.Management.Smo.Server
我有一个C#桌面应用程序,它调用各种SQL Server存储过程来执行各种导出和导入数据到SQL Server 2008 R2数据库的工作.
这些都很好,没问题.我的应用程序通过所有参数调用它们就好了.
为了"帮助用户",我正在编写一个按钮,将所有存储过程添加到配置的数据库中.为此,我创建了一个脚本:
USE [%DATABASENAME%]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spMyProc1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spMyProc1]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spMyProc2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spMyProc2]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spMyProc3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spMyProc3]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spMyProc1]
@VariousParams varchar(100), …Run Code Online (Sandbox Code Playgroud) string commandGetIslemIdleri = ("EXEC GetIslemIdleri");
cmd = new SqlCommand(commandGetIslemIdleri, sqlConn);
cmd.Parameters.Add(new SqlParameter("@CARIID", 110));
using (var reader = cmd.ExecuteReader()) //error occurs here
{
while (reader.Read())
{
islemidleri.Add(reader.GetInt32(0));
}
}
Run Code Online (Sandbox Code Playgroud)
上面是我试图编写的代码CARIID,它使用一个整数参数来调用以下存储过程。当我运行代码时,发生错误并提示,"Procedure or function 'GetIslemIdleri' expects parameter '@CARIID', which was not supplied."
但据我从此处阅读的示例了解到的,我正在使用此代码发送参数,cmd.Parameters.Add(new SqlParameter("@CARIID", 110));我需要帮助,在此先感谢您。
ALTER PROCEDURE [dbo].[GetIslemIdleri]
@CARIID int
AS
BEGIN
SET NOCOUNT ON;
SELECT ID
FROM TBLP1ISLEM
WHERE TBLP1ISLEM.CARI_ID=@CARIID
END
Run Code Online (Sandbox Code Playgroud) 我正在使用以类似于以下代码的布局编写的DAL对象.我简化了很多代码只是为了显示设置.
public class UserDatabase : IDisposable
{
private SqlDataAdapter UserDbAdapter;
private SqlCommand UserSelectCommand;
private SqlCommand UserInsertCommand;
private SqlCommand UserUpdateCommand;
private SqlCommand UserDeleteCommand;
private System.Data.SqlClient.SqlConnection SQLConnection;
public UserDatabase()
{
this.SQLConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
this.UserDbAdapter= new SqlDataAdapter();
this.UserDbAdapter.DeleteCommand = this.UserDeleteCommand;
this.UserDbAdapter.InsertCommand = this.UserInsertCommand;
this.UserDbAdapter.SelectCommand = this.UserSelectCommand;
this.UserDbAdapter.UpdateCommand = this.UserUpdateCommand;
}
private bool FillUsers(DataSet UserDataSet, out int numberOfRecords)
{
bool success = true;
numberOfRecords = 0;
string errorMsg = null;
this.UserDbAdapter.SelectCommand = this.GetUsersSelectCommand();
numberOfRecords = UserDbAdapter.Fill(UserDataSet, UsersTableName);
return success;
}
private SqlCommand GetUserSelectCommand() …Run Code Online (Sandbox Code Playgroud) 当我进入这个
INSERT INTO works_on
(essn, pno, hours)
values
('123456789', 1, 32.5),
('123456789', 2, 7.5),
('666884444', 3, 40.0),
('453453453', 1, 20.0),
('453453453', 2, 20.0),
('333445555', 2, 10.0),
('333445555', 3, 10.0),
('333445555', 10, 10.0),
('333445555', 20, 10.0),
('999887777', 30, 30.0),
('999887777', 10, 10.0),
('987987987', 10, 35.0),
('987987987', 30, 5.0),
('987654321', 30, 20.0),
('987654321', 20, 15.0),
('888665555', 20, 0);
Run Code Online (Sandbox Code Playgroud)
我得到了跟随错误
ORA-00933:SQL命令未正确结束
我有大约150万个文件需要在数据库中插入记录.每个记录都插入一个包含文件名的密钥.
问题:当前没有唯一标识的文件.
所以,我们想要做的是,对于每个文件:
我能想到的最好的事情是:
据我所知,这看起来像是:
我无法绕过实际的文件部分,但对于其他部分,是否有更好的策略我没有看到?
我有一些函数使用SqlCommand对象来进行数据插入和查询.但是一个函数(文件中的最后一个函数)似乎将(大部分)属性回显到输出中.有问题的功能:
function Add-DataStudentChangeEvent($person,
$key,
$currentValue,
$newValue,
$eventType){
$cmdEvent=New-Object System.Data.SqlClient.SqlCommand
$cmdEvent.Connection = $conn
$cmdEvent.CommandTimeout = 600000
$cmdEvent.CommandText = "INSERT INTO ChangeEvent
(AttributeKey
,CurrentAttributeValue
,NewAttributeValue
,EventType
,EventDate
,CompletedStatus
,Person_Id)
VALUES
(@AttributeKey,
@CurrentAttributeValue,
@NewAttributeValue,
@EventType,
GETDATE(),
0,
@PersonId);" -F
$cmdEvent.Parameters.AddWithValue("@AttributeKey", $key);
$cmdEvent.Parameters.AddWithValue("@CurrentAttributeValue", $current);
$cmdEvent.Parameters.AddWithValue("@NewAttributeValue", $updateTo);
$cmdEvent.Parameters.AddWithValue("@EventType", $eventType);
$cmdEvent.Parameters.AddWithValue("@PersonId", $person);
$cmdEvent.ExecuteNonQuery()
}
Run Code Online (Sandbox Code Playgroud)
在使用参数化查询的另一个类似函数中,我发现-F在最后添加,停止回声.我确实说过大部分参数.实际上只显示了5个中的4个.这是我得到的输出:
1
CompareInfo : None
XmlSchemaCollectionDatabase :
XmlSchemaCollectionOwningSchema :
XmlSchemaCollectionName :
ForceColumnEncryption : False
DbType : String
LocaleId : 0
ParameterName : @CurrentAttributeValue
Precision : 0
Scale : 0
SqlDbType …Run Code Online (Sandbox Code Playgroud) sqlcommand ×10
c# ×7
sql ×2
.net ×1
ado.net ×1
command-line ×1
file ×1
insert ×1
java ×1
oracle11g ×1
powershell ×1
process ×1
send ×1
smo ×1
sql-server ×1
sqldbtype ×1
sqlparameter ×1