我试图只使用一个连接并一起运行两个命令,一个使用事务,一个不使用.
没有跟踪/日志记录功能,因为此解决方案部署在另一个位置.因此,当流程部分失败时,我至少可以按照日志进行操作.
我将在这里添加我的测试代码:
SqlConnection connection = GetConnection();
SqlTransaction transaction = null;
try
{
connection.Open();
transaction = connection.BeginTransaction();
SqlCommand logCommand = new SqlCommand("Log before main command", connection);
logCommand.ExecuteNonQuery();
string sql = "SELECT 1";
SqlCommand command = new SqlCommand(sql, connection, transaction);
int rows = command.ExecuteNonQuery();
logCommand = new SqlCommand("Log after main command", connection);
logCommand.ExecuteNonQuery();
// Other similar code
transaction.Commit();
command.Dispose();
}
catch { /* Rollback etc */ }
finally { /* etc */ }
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
当分配给命令的连接处于挂起的本地事务中时,ExecuteNonQuery要求命令具有事务.该命令的Transaction属性尚未初始化.
如果没有其他无交易连接,有没有办法实现我想要做的事情?
或者,如果有一个更好的建议,以不同的方式通过单个连接优化我的代码,我就可以开始学习它.
我正在为我的项目添加压缩,旨在提高从Android应用程序到ASP.NET C#Server的3G数据通信的速度.
我研究/编写/测试过的方法很有效.但是,压缩后会增加空白区域.它们也有所不同.这真让我困惑.
是否与Java/ASP.NET C#中的GZIP类的不同实现有关?这是我应该关注的事情,还是在解压后我继续使用.Trim()和.trim()?
Java,压缩"玛丽有一只小羊羔"给出:
压缩数据长度:42
Base64压缩字符串:H4sIAAAAAAAAAPNNLKpUyEhMUUhUyMksKclJVchJzE0CAHrIujIWAAAA
protected static byte[] GZIPCompress(byte[] data) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gZIPOutputStream.write(data);
gZIPOutputStream.close();
return byteArrayOutputStream.toByteArray();
} catch(IOException e) {
Log.i("output", "GZIPCompress Error: " + e.getMessage());
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
ASP.NET C#,压缩"玛丽有一只小羊羔"
压缩数据长度:137
的Base64压缩字符串:H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1 + B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee ++ 999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9 + FB8/Ir7I6ut0ns3SLC2Lti3ztMwWk/8Hesi6MhYAAAA =
public static byte[] GZIPCompress(byte[] data)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
{
gZipStream.Write(data, 0, …Run Code Online (Sandbox Code Playgroud)