我从不想使用字符串操作拆分连接字符串并获取服务器,数据库,uid和密码.
我阅读了以下链接并阅读了接受的答案,我发现这是从连接字符串中获取用户ID和密码的最佳方法,但是数据库名称呢?
如何使用SqlConnectionStringBuilder从连接字符串获取数据库名称.(DataSource是服务器名称吗?)
我在这里使用遗留代码,并且有许多实例SqlDataReader
从未关闭或处置.连接已关闭,但我不确定是否有必要手动管理阅读器.
这会导致性能下降吗?
推荐的批量大小是SqlBulkCopy
多少?我正在寻找一个可用作性能调优起点的通用公式.
我刚刚开始使用ADO.NET和DataSet和DataTables.我遇到的一个问题是,在尝试调试时,似乎很难分辨数据表中的值.
有哪些最简单的方法可以快速查看DataTable中保存的值?有没有在调试时看到Visual Studio中的内容,或者是将数据写入文件的唯一选项?
我创建了一个小实用程序函数,它将DataTable写入CSV文件.然而,生成的CSV文件被切断了.应该是写出System.Guid中间最后一行的大约3行,文件就停止了.我无法判断这是我的CSV转换方法或DataTable的原始填充的问题.
更新
忘掉最后一部分,我忘了冲洗我的流作家.
在使用using() {}
如下所示的(sic)块时,并假设cmd1
它不在第一个using() {}
块的范围之外,为什么第二个块会抛出一个带有消息的异常
SqlParameter已包含在另一个SqlParameterCollection中
这是否意味着资源和/或句柄 - 包括SqlParameterCollection
附加的参数() - cmd1
在块的末尾被销毁时不会被释放?
using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
{
var parameters = new SqlParameter[] { new SqlParameter("@ProductId", SqlDbType.Int ) };
using(var cmd1 = new SqlCommand("SELECT ProductName FROM Products WHERE ProductId = @ProductId"))
{
foreach (var parameter in parameters)
{
cmd1.Parameters.Add(parameter);
}
// cmd1.Parameters.Clear(); // uncomment to save your skin!
}
using (var cmd2 = new SqlCommand("SELECT Review FROM ProductReviews WHERE ProductId …
Run Code Online (Sandbox Code Playgroud) 我有以下代码来指定SQL查询的参数.我使用时会遇到异常Code 1
; 但是当我使用时工作正常Code 2
.在Code 2
我们检查null并因此检查if..else
块.
例外:
参数化查询'(@ application_ex_id nvarchar(4000))SELECT E.application_ex_id A'需要参数'@application_ex_id',这是未提供的.
代码1:
command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
Run Code Online (Sandbox Code Playgroud)
代码2:
if (logSearch.LogID != null)
{
command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
}
else
{
command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );
}
Run Code Online (Sandbox Code Playgroud)
题
你能解释一下为什么它无法从代码1中的logSearch.LogID值中获取NULL(但能够接受DBNull)吗?
有没有更好的代码来处理这个?
参考:
码
public Collection<Log> GetLogs(LogSearch logSearch)
{
Collection<Log> logs = new Collection<Log>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string commandText = @"SELECT *
FROM Application_Ex E
WHERE (E.application_ex_id = @application_ex_id …
Run Code Online (Sandbox Code Playgroud) 我有一些C#,其中我在connection(ExecuteReader
)上创建一个阅读器,然后对于该阅读器中的每一行,执行另一个命令(with ExecuteNonQuery
).在这种情况下,我MultipleActiveResultSets=True
在连接上使用或使用多个连接更好吗?
private void button1_Click(object sender, EventArgs e)
{
string name;
name = textBox5.Text;
SqlConnection con10 = new SqlConnection("con strn");
SqlCommand cmd10 = new SqlCommand("select * from sumant where username=@name");
cmd10.Parameters.AddWithValue("@name",name);
cmd10.Connection = con10;
cmd10.Connection.Open();//line 7
SqlDataReader dr = cmd10.ExecuteReader();
}
if ( textBox2.Text == dr[2].ToString())
{
//do something;
}
Run Code Online (Sandbox Code Playgroud)
当我调试到第7行时,没关系,但之后dr会抛出异常:
Invalid attempt to read when no data is present.
这是不可能的,因为我在表中有user = sumant的数据.请告诉我"if"声明是否正确.........
我该如何删除错误?
我试图从我的C#windows应用程序调用存储过程.存储过程在SQL Server 2008的本地实例上运行.我能够调用存储过程但我无法从存储过程中检索该值.该存储过程应该返回序列中的下一个数字.我在网上做过研究,我见过的所有网站都指出这个解决方案有效.
存储过程代码:
ALTER procedure [dbo].[usp_GetNewSeqVal]
@SeqName nvarchar(255)
as
begin
declare @NewSeqVal int
set NOCOUNT ON
update AllSequences
set @NewSeqVal = CurrVal = CurrVal+Incr
where SeqName = @SeqName
if @@rowcount = 0 begin
print 'Sequence does not exist'
return
end
return @NewSeqVal
end
Run Code Online (Sandbox Code Playgroud)
调用存储过程的代码:
SqlConnection conn = new SqlConnection(getConnectionString());
conn.Open();
SqlCommand cmd = new SqlCommand(parameterStatement.getQuery(), conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
param = cmd.Parameters.Add("@SeqName", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Input;
param.Value = "SeqName";
SqlDataReader reader = cmd.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)
我也尝试使用a …
我需要在内存中使用基于来自GridView的列和方向的DataTable.该函数需要如下所示:
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
....
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助填写这个功能.我想我可以使用Select语句,但我不知道如何.由于此浏览器,我无法点击评论,但您可以向我展示一个就地或新的DataTable解决方案.对于那些向我显示指针的人来说,我需要一个类似于原型的编码功能.
怎么样:
// ds.Tables[0].DefaultView.Sort="au_fname DESC";
public static void Resort(ref DataTable dt, string colName, string direction)
{
string sortExpression = string.Format("{0} {1}", colName, direction);
dt.DefaultView.Sort = sortExpression;
}
Run Code Online (Sandbox Code Playgroud) ado.net ×10
c# ×8
.net ×5
sql-server ×4
datatable ×2
sql ×2
asp.net ×1
csv ×1
dataset ×1
debugging ×1
performance ×1
return-value ×1