我们可以System.Data.SqlClient在C#中使用库轻松使用SQL Server数据库.
我们可以从存储过程返回值,或者我们可以通过在C#代码中对它们进行硬编码来编写sql命令.
然后我们将结果传递给a SqlDataReader来读取值.
但我真的很想知道SQL Server返回C#.
C#调用SQL Server,SQL Server在返回时给C#提供了一些东西(XML,JSON或其他东西).
这件事真的是什么?如果我们想通过手动阅读它,我们怎么才能得到这个东西呢?
我刚开始使用SQL,现在我需要为我的C#应用程序创建一个数据库,它将保存用户名和密码.把它想象成密码提醒.
无论如何,我认为我需要做的是:我需要创建一个SQL数据库,该数据库将仅用于保存数据.它不应该在计算机上安装SQL Server.
我在网上搜索但没有结果,所有这些都需要使用SQL Server,请你能给我这样做的步骤,或者任何资源和非常感谢.
我有一个简单的LINQ到实体查询,如下所示:
var BillingNumbers = from o in dbcontext.Orders
where SqlFunctions.IsNumeric(o.BillingNumber) == 1
select o.BillingNumber;
Run Code Online (Sandbox Code Playgroud)
该查询大部分时间都有效。但是,最近我遇到了这样一种情况,其中BillingNumber确实是数字,但是有一些尾随空格。该查询完全错过了这些值。
如果SqlFunctions.IsNumeric()不能忽略尾随空格,还有哪些选择?
.Net 4 / EF4 / VS2010
我为Windows CE 5.0设备开发,我正在尝试连接到远程SQL Server.但是,如果我使用带参数的命令,我会得到异常:
{System.Data.SqlClient.SqlException: SqlException
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Test.MainForm.TestConnection()
...
}
Run Code Online (Sandbox Code Playgroud)
码:
SqlConnection conn = new SqlConnection(connectionString); …Run Code Online (Sandbox Code Playgroud) 想象一下,你有以下场景:
连接字符串应该如何?
我可以考虑两个选择:
Data Source.Data Source和副副本为Failover Partner我应该使用哪一个来获得高可用性和自动故障转移?其他选择?
我正在尝试使用 Microsoft.Data.SqlClient 但无法使连接正常工作。但是,当我使用 System.Data.SqlClient 时,一切正常。
try
{
using (var connection = new SC.SqlConnection(connectionString))
{
connection.Open();
connection.State.ShouldBe(ConnectionState.Open);
}
}
catch (Exception e)
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
连接字符串是:
"server=[[**SERVER_NAME**]];Trusted_Connection=True;database=[[**DATABASE**]];min pool size=0;max pool size=100;Application Name=Data.Tests.DotNet;timeout=120"
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
Microsoft.Data.SqlClient.SqlException
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: HTTP Provider, error: 0 - )
Run Code Online (Sandbox Code Playgroud)
我能找到的所有内容都在“错误:0 -”之后显示一些消息。
有人可以帮忙吗?
更新
公司环境为:
连接字符串:
"server=[[**SERVER_NAME**]];Trusted_Connection=True;database=[[**DATABASE**]];min pool …Run Code Online (Sandbox Code Playgroud) 我一直在寻找如何连接到我的本地SQL数据库的时间.我简直不敢相信这很困难.无论如何,我认为我使用此代码更加正确.
<%
Dim myConnection as System.Data.SqlClient.SqlConnection
Dim myCommand as System.Data.SqlClient.SqlCommand
myConnection = New System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=dbtest;Integrated Security=True")
myConnection.Open()
%>
Run Code Online (Sandbox Code Playgroud)
但是,它仍然无法正常工作.我不知道从哪里开始,因为我在页面加载时也收到此消息:
运行时错误
说明:服务器上发生应用程序错误.此应用程序的当前自定义错误设置可防止远程查看应用程序错误的详细信息(出于安全原因).但是,它可以由运行在本地服务器计算机上的浏览器查看.
详细信息:要在远程计算机上查看此特定错误消息的详细信息,请在位于当前Web应用程序根目录中的"web.config"配置文件中创建标记.然后,此标记应将其"mode"属性设置为"Off".
我创建了一个web.config文件,它没有改变任何东西.我迷路了.任何帮助表示赞赏!
堆栈跟踪:[SqlException(0x80131904):无法打开登录请求的数据库"dbtest".登录失败.用户'IIS APPPOOL\DefaultAppPool'登录失败.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection)+6244425 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)+245
System.Data.SqlClient .TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2811
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)+53
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,Boolean ignoreSniOpenTimeout,Int64 timerExpire,SqlConnection owningObject)+248
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host,String newPassword,Boolean redirectedUserInstance,SqlConnection owningObject,SqlConnectionString connectionOptions,Int64 timerStart)+6260362 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject,SqlConnectionString connectionOptions,String newPassword,Boolean redirectedUserInstance)+6260328 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,SqlConnection owningObject,Boolean redirectedUserInstance)+354
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,Object poolGroupProviderInfo,DbConnectionPool池,DbConnection owningConnection)+703
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection,DbConnectionPool池,DbConnectionOptions选项)+54 System.Data.ProviderBase .DbConnectionPool.CreateObject(DbConnection owningObject)+6261592
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)+81
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)+1657
System.Data.ProviderBase.DbConnectionFactory.GetConnection( …
我从数据库(1条记录)中提取数据并将每个值放在文本框或下拉列表中.其中一些值是DB.null.如果是,我将每个转换为""(空白).但是,一旦用户修改了文本框/下拉列表,他们就可以"保存"新结果.我的问题是:
如果用户留下空白,我希望能够保持该值为DB.null.有没有办法做到这一点?
另外,如果文本框中有值并且用户决定删除它,我是否可以将其转换为DB.Null?这是我试图给你们一些想法的事情:
productName != ""
? myCommand.Parameters.AddWithValue("@ProductName", productName)
: myCommand.Parameters.AddWithValue("@ProductName", DBNull.Value);
Run Code Online (Sandbox Code Playgroud) 我一直在处理这个问题.我有一个简单的PowerShell函数,它接受SQL Server的开放连接和包含SQL命令的String,并对服务器运行它.该函数应返回一个整数值,表示已更新的行数.但是,它正在输出有关该方法的信息.
码:
function Invoke-MSSQLNonQuery () {
param (
[System.Data.SqlClient.SqlConnection]$Connection,
[string]$Command
)
# Create the SQL Command Ojbect
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
# Set the Command Text and Connection for the Command
$SQLCommand.CommandText=$Command
$SQLCommand.Connection=$Connection
# Run command and return the rows affected
[int]($SQLCommand.ExecuteNonQuery | Out-String)
}
Run Code Online (Sandbox Code Playgroud)
但是,执行时,我收到以下错误:
Cannot convert value "
OverloadDefinitions
-------------------
int ExecuteNonQuery()
int IDbCommand.ExecuteNonQuery()
" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\_Local\playground\Mason\UpdateFromInventory.ps1:153 char:5
+ [int]($SQLCommand.ExecuteNonQuery | Out-String)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ …Run Code Online (Sandbox Code Playgroud) 我们在 .NET Framework 4.7.2 应用程序中使用Application Insights SDK (v2.14),并且能够成功跟踪 SQL 依赖项(尽管没有查询文本)。
我们现在正在从 System.Data.SqlClient 迁移到Microsoft.Data.SqlClient包,因此我们还可以进行高级 SQL 跟踪以查看遥测中的完整查询文本。我们更新了 ApplicationInsights.config 文件以将EnableSqlCommandTextInstrumentation设置为true。
但是,安装最新的 Microsoft.Data.SqlClient 包(撰写本文时为 2.01)后,我们不再使用客户端跟踪代码的任何 SQL 依赖项。
有什么问题吗?
sqlclient ×10
c# ×6
sql ×3
.net ×2
ado.net ×2
asp.net ×2
sql-server ×2
c#-4.0 ×1
connection ×1
database ×1
exception ×1
legacy ×1
powershell ×1
return-type ×1
t-sql ×1
windows-ce ×1