VS Express 2017 w / SQL Sever 2017 Express - 从应用程序内部连接到DB [英] VS Express 2017 w/ SQL Sever 2017 Express - Connecting to DB from within application

查看:71
本文介绍了VS Express 2017 w / SQL Sever 2017 Express - 从应用程序内部连接到DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对VS很新。 从VS内成功创建了一个简单的数据库。


可以在VS内修改,添加,等等没问题。


但是..当我尝试从代码中访问.mdf文件时。


连接字符串是: 


SqlConnection ewrDbConn = new SqlConnection("数据来源=。\\SQLEXPRESS;" +

                 ;"初始目录= EwrDatabase;" +

               " ;集成安全性= SSPI;" +

               " User ID = DESKTOP-98LMVHI \\ Ggraphics;" +

                " Connect Timeout = 10;");


我得到:  System.Data.SqlClient.SqlException发生了
  HResult = 0x80131904

 消息=无法打开数据库"EwrDatabase"登录请求。登录失败。

用户'DESKTOP-98LMVHI \ Ggraphics'登录失败。

  Source = .Net SqlClient数据提供者

  StackTrace:

   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,SqlCredential credential,Object providerInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,
SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool pool,String accessToken,Boolean applyTransientFaultHandling)

  在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,对象poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection,DbConnectionOptions userOptions)

  在System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool池,DbConnection owningObject,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)

  在System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)

  在System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)

  在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal& connection)

  在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource`1 retry,DbConnectionOptions userOptions,DbConnectionInternal& connection)

  在System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource`1 retry,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& connection)

  在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource`1 retry,DbConnectionOptions userOptions)

  在System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource`1 retry,DbConnectionOptions userOptions)

  在System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1重试)

  在System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1重试)

  在System.Data.SqlClient.SqlConnection.Open()

   at Ewr_Application_Forms_v1.Form1.button1_Click(Object sender,EventArgs e)在C:\ Users \ Ggraphics \ source \repos\Ewr_Application_Forms_v1 \Ewr_Application_Forms_v1 \ Engineering工程RequestcApp.cs:第80行¥b $ b  在System.Windows.Forms.Control.OnClick(EventArgs e)

  在System.Windows.Forms.Button.OnClick(EventArgs e)

  在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

  在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)

  在System.Windows.Forms.Control.WndProc(消息& m)

  在System.Windows.Forms.ButtonBase.WndProc(消息& m)

  在System.Windows.Forms.Button.WndProc(消息& m)

  在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)

  在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)

  在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

  在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)

  在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)

  在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)

  在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)

  在System.Windows.Forms.Application.Run(Form mainForm)

   at Ewr_Application_Forms_v1.Program.Main()in C:\ Users \ Ggraphics\source \ _reposh \\\\\\在那里?


签名'迷失在MS-VS文档中!'

解决方案

Hi VslnSTL,


欢迎来到MSDN论坛。


>>从VS内成功创建了一个简单的数据库。


你的申请类型是什么?由于我们的论坛是讨论VS IDE而您的问题是关于开发问题,请提供更多详细信息,我将
帮助您搜索适当的论坛以获得更好的支持,谢谢您的理解。


> ;>但是......当我尝试从代码中访问.mdf文件时。


请查看

this
并调整你的连接字符串或检查帐户'DESKTOP-98LMVHI \\ Ggraphics'有足够的权限,或者去server explorer>您的数据库>右键单击>属性> ConnectionString并复制连接字符串并将
复制到connectiongstring代码。





最好的问候,


萨拉


I'm fairly new to VS.  Successfully created a simple database from within VS.

Can alter in, add to it, etc. with no problem within VS.

BUT... when I try to access the .mdf file from within code.

Connection string is: 

SqlConnection ewrDbConn = new SqlConnection("Data Source=.\\SQLEXPRESS;" +
                " Initial Catalog=EwrDatabase;" +
                " Integrated Security=SSPI;" +
                " User ID=DESKTOP-98LMVHI\\Ggraphics;" +
                " Connect Timeout=10;");

I get: System.Data.SqlClient.SqlException occurred
  HResult=0x80131904
  Message=Cannot open database "EwrDatabase" requested by the login. The login failed.
Login failed for user 'DESKTOP-98LMVHI\Ggraphics'.
  Source=.Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Ewr_Application_Forms_v1.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Ggraphics\source\repos\Ewr_Application_Forms_v1\Ewr_Application_Forms_v1\Engineering Work RequestcApp.cs:line 80
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Ewr_Application_Forms_v1.Program.Main() in C:\Users\Ggraphics\source\repos\Ewr_Application_Forms_v1\Ewr_Application_Forms_v1\Program.cs:line 19

Any help out there?

Signed 'Lost in MS-VS Documentation!'

解决方案

Hi VslnSTL,

Welcome to the MSDN forum.

>> Successfully created a simple database from within VS.

What’s your application type? Since our forum is to discuss the VS IDE and your issue is about the development issue, please provide more detail information and I will help you search an appropriate forum to get a better support, thank you for your understanding.

>> BUT... when I try to access the .mdf file from within code.

Please have a look at this and adjust your connection string or check the account ‘DESKTOP-98LMVHI\\Ggraphics’ have the enough permission, or go to server explorer > Your Database > Right Click > properties > ConnectionString and copy the connection string and past the copied to connectiongstring code.

Best regards,

Sara


这篇关于VS Express 2017 w / SQL Sever 2017 Express - 从应用程序内部连接到DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆