小编Pau*_*jto的帖子

如何从对象中取消对它包含的类型,在编译时不知道该类型?

在运行时,我得到某种类型的盒装实例.如何将其拆箱到基础类型?

Object obj;
String variable = "Some text";

obj = variable // boxing;

// explicit unboxing, because we know the type of variable at compile time.

var x = (String)obj     

// Now let's pretend that we don't know the type of underlying object at compile time. 

Type desiredType = obj.GetType(); // But we can figure out.

//And now the question. 
//How to express something like this:

var y = (desiredType)obj; //Need to get unboxed instance of initial variable here; 
Run Code Online (Sandbox Code Playgroud)

c# unboxing

26
推荐指数
3
解决办法
1万
查看次数

从作业执行的sp_send_dbmail失败,查询结果作为文件附加

我遇到了以下问题:当尝试发送带有作为文件附加查询结果的电子邮件时,通过执行普通查询使用sp_send_dbmail一切似乎都正常.

但是,如果将相同的代码添加到JobStep并运行作业,则会失败.

工作经历中的错误说

格式化查询时出错,可能是无效参数[SQLSTATE 42000](错误22050).步骤失败了.

但是当我注释掉引用文件的参数时,它再次开始正常工作.

exec msdb.dbo.sp_send_dbmail 
    @profile_name = 'profile_name', 
    @recipients  = 'some@mail.com',
    @body = 'body',
    @subject = 'subj',
    --Parameters that refers to attached file
    @attach_query_result_as_file = 1, 
    @query_result_header = 0,
    @query_result_no_padding = 1,
    @query = 'select 1',
    @query_attachment_filename = 'test.csv'
Run Code Online (Sandbox Code Playgroud)

有什么建议?

sql-server sp-send-dbmail attachment sql-server-2008

17
推荐指数
2
解决办法
5万
查看次数

TransactionScope在处置之前中止了交易

当使用TransactionScope时,它表示如果内部执行的代码回滚事务,那么父事务也将回滚.这对我有好处.但是当处置该范围时,它会抛出异常,这意味着事务已经回滚并且已中止.那么处理这个并正确处理范围的正确方法是什么?

    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
    {
                    using (var conn = GetConnection())
                    {
                            string query = 
              @"some query that may contain transaction itself 
              or some SP whith transaction included"

                            using (var command = new SqlCommand(query, conn))
                                command.ExecuteNonQuery();
                        }
                    }
                    scope.Complete();
    } // Exception here
Run Code Online (Sandbox Code Playgroud)

c# sql transactions

7
推荐指数
1
解决办法
1万
查看次数

如何更改sp_send_dbmail查询的附件编码?

我用来sp_send_dbmail生成和发送提供给其他程序的文件。该程序消化“ANSI/ASCII”和“ISO-8859-1”编码。但我无法sp_send_dbmail制作一个。

过程调用看起来像这样

exec msdb.dbo.sp_send_dbmail 
    @profile_name= @profile_name, 
    @recipients  = @recipients,
    @body = @body,
    @subject = @subject,
    @attach_query_result_as_file = 1, 
    @query_result_header = 0,
    @query_result_no_padding = 1,
    @query = @query,
    @query_attachment_filename = @fname,
    @query_result_width = 4000,
    @mailitem_id = @mailitem_id OUTPUT
Run Code Online (Sandbox Code Playgroud)

因此附件是根据传递的查询结果创建的。但由于某种原因实际附加到邮件的结果文件是用 UCS2 Little Endian 编码的。有办法改变吗?

sql email encoding sp-send-dbmail

1
推荐指数
1
解决办法
5814
查看次数