从 blob 存储 Azure 下载文件时无法执行查询
Can't Execute Query when downloading a file from blob storage Azure
我正在尝试从 blob 容器下载 SQL 文件。下载成功,我得到了文件,问题出在编码上,当我执行命令 ExecuteNonQuery 时,数据库给出了一个错误,说创建表时出错。我已经调试了我的方式,发现我下载文件后得到的字符串包含“\n”
BlobAccess.cs
public string GetBlob(string fileName)
{
string connectionString ="constring";
// Setup the connection to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference("storage");
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference($"{fileName}");
// Get the blob file as text
var blobRequestOptions = new BlobRequestOptions
{
MaximumExecutionTime = TimeSpan.FromMinutes(15),
ServerTimeout = TimeSpan.FromHours(1)
};
string contents = blob.DownloadTextAsync(Encoding.UTF8, null, blobRequestOptions, null).Result;
return contents;
}
Program.cs
try
{
string scripttocreatetables = blobAccess.GetBlob("test.sql");
var conn = new SqlConnection(dbstring);
try
{
conn.Open();
var cmd = new SqlCommand("query", conn);
cmd.CommandText = scripttocreatetables;
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
var mh = new MonitorHelper(vcCustomer);
var itemid = mh.SetStart("Database");
mh.SetError(itemid, "Database connection failed: " + e.Message);
mh.SetEnd(itemid, 0, 0);
Console.WriteLine(e.Message);
conn.Close();
return;
}
conn.Close();
}
catch (Exception e)
{
}
我尝试在我的实验室中重现该场景,但找不到您遇到的问题。
我尝试从 blob 下载 SQL 文件(也尝试使用 txt 文件)获取正确的数据没有“\n”
SQL 文件的输出
从 SQL 文件中获取数据后,将其传递给 CommandText 以执行 query.Try 此代码以执行查询
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
我正在尝试从 blob 容器下载 SQL 文件。下载成功,我得到了文件,问题出在编码上,当我执行命令 ExecuteNonQuery 时,数据库给出了一个错误,说创建表时出错。我已经调试了我的方式,发现我下载文件后得到的字符串包含“\n”
BlobAccess.cs
public string GetBlob(string fileName)
{
string connectionString ="constring";
// Setup the connection to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference("storage");
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference($"{fileName}");
// Get the blob file as text
var blobRequestOptions = new BlobRequestOptions
{
MaximumExecutionTime = TimeSpan.FromMinutes(15),
ServerTimeout = TimeSpan.FromHours(1)
};
string contents = blob.DownloadTextAsync(Encoding.UTF8, null, blobRequestOptions, null).Result;
return contents;
}
Program.cs
try
{
string scripttocreatetables = blobAccess.GetBlob("test.sql");
var conn = new SqlConnection(dbstring);
try
{
conn.Open();
var cmd = new SqlCommand("query", conn);
cmd.CommandText = scripttocreatetables;
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
var mh = new MonitorHelper(vcCustomer);
var itemid = mh.SetStart("Database");
mh.SetError(itemid, "Database connection failed: " + e.Message);
mh.SetEnd(itemid, 0, 0);
Console.WriteLine(e.Message);
conn.Close();
return;
}
conn.Close();
}
catch (Exception e)
{
}
我尝试在我的实验室中重现该场景,但找不到您遇到的问题。
我尝试从 blob 下载 SQL 文件(也尝试使用 txt 文件)获取正确的数据没有“\n”
SQL 文件的输出
从 SQL 文件中获取数据后,将其传递给 CommandText 以执行 query.Try 此代码以执行查询
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}