Env*_*vil 2 c# sql-server filestream
我有一个启用文件流的数据库
我能够写入数据库,但当我尝试读回数据时,我得到了这个错误
System.Data.dll中发生未处理的"System.ComponentModel.Win32Exception"类型的异常附加信息:用户名或密码不正确
我试图执行的代码就是这个
public void ReadFromDatabase()
{
using (SqlConnection connection = new SqlConnection(sql_ConnectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP(1) Video.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM Library", connection);
SqlTransaction sqlTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = sqlTrans;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string path = reader.GetString(0);
byte[] transContext = reader.GetSqlBytes(1).Buffer;
using (Stream fileStream = new SqlFileStream(path, transContext, FileAccess.Read, FileOptions.SequentialScan, allocationSize: 0))
{
byte[] data = new byte[fileStream.Length];
fileStream.Read(data, 0, (int)fileStream.Length);
File.WriteAllBytes(@"C:\Users\Georgi\AppData\Local\VideoPresenter\temp.mp4", data);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用集成安全性,因此用户和密码保持不变并硬编码到应用程序中.
这是连接字符串代码
//Create a string builder object
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
//Set the properties of the builder
builder.AsynchronousProcessing = false;
builder.DataSource = "xxx.xxx.xxx.xxx";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "VideoLibrary";
//Set the connection string and connection objects' data
sql_ConnectionString = builder.ToString();
//try to connect to the server
Run Code Online (Sandbox Code Playgroud)
然后我只是使用字符串打开连接
传递的连接字符串是
"数据源= xxx.xxx.xxx.xxx;初始目录= VideoLibrary;集成安全性= True"
这是堆栈跟踪
System.Data.dll!System.Data.SqlTypes.SqlFileStream.OpenSqlFileStream(string path, byte[] transactionContext, System.IO.FileAccess access, System.IO.FileOptions options, long allocationSize) Unknown
System.Data.dll!System.Data.SqlTypes.SqlFileStream.SqlFileStream(string path, byte[] transactionContext, System.IO.FileAccess access, System.IO.FileOptions options, long allocationSize) Unknown
Run Code Online (Sandbox Code Playgroud)
SQL FILESTREAM TEST CLIENT.exe!SQL_FILESTREAM_TEST_CLIENT.SQLOperations.ReadFromDatabase()第158行C#SQL FILESTREAM测试CLIENT.exe!SQL_FILESTREAM_TEST_CLIENT.MainWindow.GetButton_Click(对象发送者,System.Windows.RoutedEventArgs e)第47行C#PresentationCore.dll!System.Windows .RoutedEventHandlerInfo.InvokeHandler(对象目标,System.Windows.RoutedEventArgs routedEventArgs)未知的PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(对象源,System.Windows.RoutedEventArgs args,bool reRaised)未知的PresentationCore.dll!System.Windows. UIElement.RaiseEventImpl(System.Windows.DependencyObject sender,System.Windows.RoutedEventArgs args)Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs e)Unknown PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnClick()未知的PresentationFramework.dll!System.Windows.Controls.Button.OnClick()未知的PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input. MouseButtonEventArgs e)未知的PresentationCore.dll!System.Windows.UIElement.OnMouseLeftButtonUpThunk(对象发送者,System.Windows.Input.MouseButtonEventArgs e)未知的PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler,object genericTarget )未知的PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler,object target)Unknown PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target,System.Windows.RoutedEventArgs routedEventArgs)未知的PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(对象源,System.Windows.RoutedEventArgs args,bool reRaised)未知的PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender,System .Windows.RoutedEventArgs args,System.Windows.RoutedEvent newEvent)Unknown PresentationCore.dll!System.Windows.UIElement.OnMouseUpThunk(object sender,System.Windows.Input.MouseButtonEventArgs e)Unknown PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs .InvokeEventHandler(System.Delegate genericHandler,object genericTarget)Unknown PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler,object target)Unknown PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,System.Windows.RoutedEventArgs routedEventArgs)未知PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(对象源,System.Windows.RoutedEventArgs args,bool reRaised)未知PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl (System.Windows.DependencyObject sender,System.Windows.RoutedEventArgs args)Unknown PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args)Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System. Windows.RoutedEventArgs args,bool trusted)未知的PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea()未知的PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs输入)未知的PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport)未知的PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd,System.Windows .Input.InputMode mode,int timestamp,System.Windows.Input.RawMouseActions actions,int x,int y,int wheel)Unknown PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd,MS.Internal .Interop.WindowMessage msg,System.IntPtr wParam,System.IntPtr lParam,ref bool处理)未知的PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd,int msg,System.IntPtr wParam,System. IntPtr lParam,ref bool处理)未知的WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam,ref bool处理)未知的WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o)未知的WindowsBase.dll!System.Windows.Threading .ExceptionWrapper.InternalRealCall(System.Delegate callback,object args,int numArgs)Unknown WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source,System.Delegate method,object args,int numArgs,System.Delegate catchHandler)未知的WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority,System.TimeSpan timeout,System.Delegate method,object args,int numArgs)Unknown WindowsBase.dll!MS.Win32.HwndSubclass .SubclassWndProc(System.IntPtr hwnd,int msg,System.IntPtr wParam,System.IntPtr lParam)未知[原生到托管过渡]
[管理到原生过渡]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame)未知的WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame)未知的WindowsBase. DLL!System.Windows.Threading.Dispatcher.Run()Unknown PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore)Unknown PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window)Unknown PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window)Unknown PresentationFramework.dll!System.Windows.Application.Run()未知的SQL FILESTREAM TEST CLIENT.exe!SQL_FILESTREAM_TEST_CLIENT.App.Main()C#[原生于管理过渡]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,System.Security.Policy.Evidence assemblySecurity,string [] args)Unknown Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()Unknown mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)未知mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,对象状态,bool preserveSyncCtx)未知mscorlib .dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)unknown mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback回调,对象状态)未知mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()未知
当我打开Management Studio时,我看到填充了数据的行,但我无法从应用程序中读取它
注意:只有默认的SQL Server用户'sa'.我也尝试过使用它,但结果是一样的.我使用Windows用户创建数据库,而不是使用'sa'创建数据库
注意:如果我在连接字符串中运行带有DataSource =(local)的代码,它可以工作.
注意:数据库处于READ_WRITE模式
我尝试过的:
我尝试重新创建表和整个数据库
我尝试重新安装SQL Server Express
我尝试向Windows添加功能,特别是Internet信息服务 - >万维网服务 - >安全 - > Windows身份验证/基本身份验证
我尝试操纵我正在使用的用户的权限,但它是一个dbo所以应该没有问题.
我尝试在不同的计算机上运行相同的代码(使用相同的Microsoft帐户)*
我尝试设置尝试设置FILESTREAM(NON_TRANSACTED_ACCESS = FULL)
**我知道FileStream只能使用Windows身份验证,而我使用的是Windows 8所以在帐户方面一切都是相同的(或者至少应该是这样)**我错过了什么?
您获得a Win32Exception而不是a 的事实SqlException表明当您打开FILESTREAM时会出现问题.您需要采取许多步骤来确保通过Win32 API进行远程FILESTREAM访问(这是您尝试执行的操作).重要的更改是在服务器上为端口445启用防火墙(请参阅为FILESTREAM访问配置防火墙)和(最重要的是)将服务器配置为允许远程客户端具有对FILESTREAM数据的流式访问.SQL配置文件流访问级别必须设置为2.
如果您仍有问题,我还建议您浏览Filestream存储白皮书.我已经看到神秘的FILESTREAM访问被拒绝错误的案例最终被诊断为管理员搞乱文件流共享文件夹存储权限.
我希望您已经了解FILESTREAM 只能使用集成安全性进行访问.必须使用集成安全性来获取句柄路径/上下文(您的SELECT语句),并且在使用Win32 API访问时(即,当您尝试打开SqlFileStream对象时)也将在后台使用.
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |