我想加载一个记录列表给出一个可能很长的用户名列表(从一个到几千个用户名).忽略如何选择名称,并假设无法从数据库中的任何现有数据确定这些名称.这适用于SQL Server 2005.
我特别想避免在where子句中使用带有数千个表达式的单个select语句,这会导致SqlCommand对象的命令文本过长(例如...where n='bob000001' or n='bob000002' or ... or n='bob003000').听起来合理吗?
我决定通过使用用户名填充一个简单的表变量来执行选择,然后使用用户数据在表变量和表之间执行select/join.
所以,我需要做的第一件事是填充表变量.我有一些问题:
第三点是我现在要解决的问题.我已经看过人们(声称)成功声明并在单个SqlCommand中使用变量而没有错误的示例.使用多个SqlCommand实例时如何实现?我读到变量将持续存在于多个命令之间的单个连接.涉及交易可能会以某种方式帮助吗?
最后,请记住我不想使用临时表; 这样做会提供一个简单的解决方案,但它也避免了我要问的有关变量和多个SqlCommands的问题; 但是,如果你真的认为这是最好的选择,请随意说出来.
这是一个代码片段,显示正在发生的事情:
public static List<Student> Load( SqlConnection conn, List<StudentID> usernames )
{
//Create table variable
SqlCommand command = new SqlCommand( "declare @s table (id varchar(30))", conn );
command.ExecuteNonQuery();
//Populate a table variable with the usernames to load
command = new SqlCommand( "insert into @s (id) …Run Code Online (Sandbox Code Playgroud) 我正在使用SqlClient.SqlCommand对象在我的数据库上运行一些存储过程.通常,当我通过查询分析器手动运行它们时,最多需要15分钟才能完成.
很明显,当我使用SqlCommand对象运行它时,我得到一个SqlCommand超时.
我知道我可以将超时属性设置为一个非常高的数字,但我想知道是否有一种方法可以启动过程并断开连接.我不需要返回值.
有什么建议?我不想在这里优化每个sp的处理时间,只是试图确定是否有"设置并忘记它"选项.
这是SqlCommand对象的内容:
using (SqlCommand cmd = new SqlCommand(@"update tbl_SYS_DATE set RPT_DATE = @inputDate
exec sp_Get_Bp_Data1
exec sp_Get_Bp_Data2
exec sp_Get_Bp_Data3
exec sp_channel_data1
exec sp_channel_data2
exec sp_channel_data3
", con))
Run Code Online (Sandbox Code Playgroud) 我在基于c#的asp.net页面上有一个SqlCommand对象.SQL和传递的参数在大多数情况下都在工作.我有一个案例不起作用,我收到以下错误:
字符串或二进制数据将被截断.该语句已终止.
我理解错误,但数据库中的所有列都应足够长,以容纳所有发送的内容.
我的问题,
有没有办法看到发送到数据库的实际SQL是从SqlCommand对象?我希望能够在发生错误时通过电子邮件发送SQL.
谢谢,贾斯汀
我正在使用带有单个SqlDbType.Structured参数的ADO.NET SqlCommand将表值参数发送到sproc.sproc返回许多行,我需要进入强类型列表.将结果集(DataTable是否从DataAdapter或DataReader位转换)到List中的最佳方法是什么?
谢谢.
这是代码:
string ConnectionString= @"Data Source=localhost\SQLEXPRESS;
Initial Catalog=notepad; Integrated Security=SSPI ";
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string strEvent = TextBoxEvent.Text;
string strDate = Calendar1.TodaysDate.ToShortDateString();
string strInsert = "insert into notepad (time, event) values (strDate, strEvent )";
SqlCommand cmd=new SqlCommand(strInsert, con);
cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
时间是smalldatetime在SQL Server 2005中
当我运行此程序时,会发生如下错误:
在此上下文中不允许使用名称"strDate".有效表达式是常量,常量表达式和(在某些上下文中)变量.不允许使用列名.
但如果我代替strDate用2010/05/22这样的:
string strInsert = "insert into notepad (time, event) values ("2010/05/22", strEvent )";
Run Code Online (Sandbox Code Playgroud)
该程序将正确运行.
我对这个问题感到困惑,并向你寻求帮助.
这个foreach循环在测试时运行良好,只返回5行数据,但我很清楚它是如何编写的,有没有更好的方法,可能使用stringbuilder来更有效地重写它?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);
SqlCommand comm = new SqlCommand("SELECT Title, StartDate FROM tblEvents JOIN eo_UserEventWatch ON eo_UserEventWatch.EventID=tblEvents.ID WHERE eo_UserEventWatch.UserID = @GUID ;", conn);
comm.Parameters.AddWithValue("GUID", userID);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
string result ="{ \"event\" :[";
foreach (DataRow dr in dt.Rows)
{
result += "{\"title\" : \"" + dr[0].ToString() + "\" , \"start\" : \"" + dr[1].ToString() +"\"} ,";
}
result = result.TrimEnd(',');
result += "] }";
return result;
Run Code Online (Sandbox Code Playgroud) 我有以下代码.
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
connection.Open();
SqlCommand select = new SqlCommand("SELECT RTRIM(LTRIM(PART_NO)) AS PART_NO, record FROM [RMAData].[dbo].[IMPORTING_ORDER_EDI] WHERE sessionID = '" + Session.SessionID + "'", connection);
SqlDataReader reader = select.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
if (!currentPart.IsActive)
{
// this part is not active, set the active flag in sql to 0
SqlCommand update = new SqlCommand("UPDATE [RMAData].[dbo].[IMPORTING_ORDER_EDI] SET valid = 0, active = 0 WHERE record = " + reader["record"].ToString() + ";", connection);
update.ExecuteNonQuery();
} …Run Code Online (Sandbox Code Playgroud) 我有一个问题,返回的值SqlCommand,我有这个代码:
string sqlSelect = "Select TOP 1 Quotation.SentToSupp as SentToSupp FROM Quotation JOIN Notifications ON Quotation.QuotationId = QuotationID ";
SqlCommand Comm = new SqlCommand(sqlSelect, this.Connection);
Run Code Online (Sandbox Code Playgroud)
sqlSelect查询选择第一个DateTime值.当我打电话给SqlCommand我想要获得这个值(只有一个值).我添加查询和我的连接很好.
但我不知道如何获得我的DateTime价值......必须使用类似的东西ExecuteReader?
先感谢您!!
我在C#中有一个程序,我创建了各种类,所有这些都需要调用数据库.所以我决定创建一个静态类来处理所有调用,以便我可以非常轻松地应用深远的变化.在那个类中,我有调用SqlCommand(ExecuteReader()等等)各个部分的函数我最终厌倦了重写所有的连接代码,并将它抽象到另一个函数中getSqlCommand(),该函数只返回一个初始化的函数SqlCommand,用于执行各种命令.我选择不将其传递SqlCommand回其他程序(虽然它是一种公共方法,以防万一我需要它),因为如果你在将结果返回给程序之前我会做一些预处理.
我的问题是,如果/何时SqlConnection关闭?因为它作为a中的返回值被传回SqlCommand,它会保持打开状态吗?如果是的话,我可以关闭吗?这是一些代码(略微剥离),所以你可以看到我在做什么,提前谢谢!
创建的功能SqlCommand:
public static SqlCommand GetSqlCommand(string query)
{
using (SqlConnection dbConnection = new SqlConnection( SQLConn )) {
dbConnection.Open();
SqlCommand cmd = new SqlCommand( query, dbConnection );
cmd.CommandTimeout = 0;
return cmd;
}
}
Run Code Online (Sandbox Code Playgroud)
静态类中命令用法的示例:
public static SqlDataReader executeReader( string query )
{
try {
return GetSqlCommand(query).ExecuteReader();
} catch (SqlException) {
//Notify User and Resolve Error
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 尝试只返回前几行,因为我的数据库太大了,但是当我测试我的SQL时,我做了一个select *并且只返回了第一行.
SqlCommand sqlCmd = new SqlCommand();
SqlDataReader reader;
sqlCmd.CommandText = "SELECT * FROM Log";
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Connection = myConnection;
myConnection.Open();
reader = sqlCmd.ExecuteReader();
Log logs = null;
while (reader.Read())
{
logs = new Log();
logs.Id = Convert.ToInt32(reader.GetValue(0));
}
return logs;
myConnection.Close();
Run Code Online (Sandbox Code Playgroud)
我的解决方案有什么问题?
sqlcommand ×10
c# ×8
asp.net ×4
sql-server ×4
sql ×3
.net ×1
connection ×1
linq-to-sql ×1
list ×1
return-value ×1
scope ×1
sqldbtype ×1
variables ×1