我试图在ASP.net核心中使用SQLClient库,但似乎无法使其工作.我发现这篇文章在线建议如何设置,但它不适合我:http://blog.developers.ba/using-classic-ado-net-in-asp-net-vnext/
我有一个简单的控制台应用程序包.我的project.json看起来像这样:
{
"version": "1.0.0-*",
"description": "DBTest Console Application",
"authors": [ "" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"System.Data.Common": "4.0.1-beta-23516",
"System.Data.SqlClient" : "4.0.0-beta-23516"
},
"commands": {
"DBTest": "DBTest"
},
"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试以下代码:
using System;
using System.Data.SqlClient;
namespace DBTest
{
public class Program
{
public static void …Run Code Online (Sandbox Code Playgroud) 当我运行 dotnet ef update 数据库时出现此错误:
A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)
Run Code Online (Sandbox Code Playgroud)
我的 csproj 文件:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.5.22302.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-preview.5.22302.2">
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0-preview.5.22302.2" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
我的连接字符串:
"DefaultConnectionString": "Server=.;Database=ShopCore;Trusted_Connection=True;"
Run Code Online (Sandbox Code Playgroud) 我是 SQl 的 100% 新手,想使用数据库制作一个 ConsoleApp。我读了一些关于它并尝试过。当我需要创建 SqlConnection 时,我的 VS 2019 Preview 向我展示了这个
严重性代码描述项目文件行抑制状态错误 CS1069 在命名空间“System.Data.SqlClient”中找不到类型名称“SqlConnection”。此类型已转发到程序集“System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” 考虑添加对该程序集的引用。ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12 活动
我不明白为什么它不起作用
这是我的代码
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
Run Code Online (Sandbox Code Playgroud) 使用Entity Framework,我昨晚在我的一个应用程序中收到了以下一些例外情况:
System.Data.EntityException: The underlying provider failed on Commit. --->
System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior
to completion of the operation or the server is not responding.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at …Run Code Online (Sandbox Code Playgroud) 假设我们有一个存储过程就像这样:
BEGIN TRANSACTION
UPDATE sometable SET aField = 0 WHERE anotherField = 1;
UPDATE sometable SET aField = 1 WHERE anotherField = 2;
ROLLBACK TRANSACTION;
Run Code Online (Sandbox Code Playgroud)
从C#我们有这样的事情:
using (var connection = new SqlConnection("connection string"))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "my_procedure";
var res = cmd.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)
为什么我没有得到res == -1?我仍然得到受影响的行数.当文档声明"如果发生回滚,返回值也为-1"
我在这里缺少什么?
我正在构建一个自定义数据库部署实用程序,我需要读取包含sql脚本的文本文件并对数据库执行它们.
非常简单的东西,到目前为止一切顺利.
但是我遇到了一个障碍,文件的内容被成功完整地读取,但是一旦传入SqlCommand然后用SqlCommand.ExecuteNonQuery执行,只执行部分脚本.
我启动了Profiler并确认我的代码没有传递所有脚本.
private void ExecuteScript(string cmd, SqlConnection sqlConn, SqlTransaction trans)
{
SqlCommand sqlCmd = new SqlCommand(cmd, sqlConn, trans);
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandTimeout = 9000000; // for testing
sqlCmd.ExecuteNonQuery();
}
// I call it like this, readDMLScript contains 543 lines of T-SQL
string readDMLScript = ReadFile(dmlFile);
ExecuteScript(readDMLScript, sqlConn, trans);
Run Code Online (Sandbox Code Playgroud) 我有一个运行存储过程的C#程序.如果我从Microsoft SQL Server管理工作室运行存储过程,它工作正常.执行大约需要30秒.但是,如果我尝试从C#程序运行相同的存储过程,它会超时,即使我已将连接字符串中的超时设置为10分钟.
using (connection1 = new SqlConnection("user id=user_id_goes_here;password=password_goes_here;initial catalog=database_name_goes_here;data source=server_name_goes_here;connection timeout=600))
Run Code Online (Sandbox Code Playgroud)
它似乎在大约30秒后超时,即使我已将其设置为允许10分钟(用于测试目的).
默认的CommandTimeout值为30秒.您可以通过执行以下操作手动更改命令对象实例上的值
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandTimeout = 60
Run Code Online (Sandbox Code Playgroud)
有没有办法指定不同的默认值,以便所有新命令对象在创建时在解决方案中自动具有此值?
使用ASP.NET MVC和C#,如何将一些数据库记录传递给View并以表格形式显示它们?
我需要知道如何从已返回到SqlDataReader对象的数据库传输/传递一些记录行,并将该对象传递给View,这样我就可以使用foreach显示View中对象所包含的所有记录.
以下代码是我正在尝试做的事情.但它不起作用.
控制者:
public ActionResult Students()
{
String connectionString = "<THE CONNECTION STRING HERE>";
String sql = "SELECT * FROM students";
SqlCommand cmd = new SqlCommand(sql, connectionString);
using(SqlConnection connectionString = new SqlConnection(connectionString))
{
connectionString.Open();
SqlDataReader rdr = cmd.ExecuteReader();
}
ViewData.Add("students", rdr);
return View();
}
Run Code Online (Sandbox Code Playgroud)
风景:
<h1>Student</h1>
<table>
<!-- How do I display the records here? -->
</table>
Run Code Online (Sandbox Code Playgroud) 我有一个.NET Standard 2.0类库项目,安装了Nuget包System.Data.SqlClient版本4.4.0和一个Windows Form .NET Framework 4.7项目,该项目引用了该类库.
安装Nuget Package并构建解决方案是成功的.但在运行时,每次代码到达一个方法,其中包含SqlClient程序集中的任何东西(例如来自SqlConnection的实例),它会收到此错误:
无法加载文件或程序集'System.Data.SqlClient,Version = 4.2.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一.该系统找不到指定的文件.
考虑到这个问题,我认为问题也出现在Nuget包的最后一个主要版本中.
编辑
我下载了Nuget软件包并解压缩,并从\ ref \netstandard2.0文件夹中手动复制了System.DataSqlClient.dll在我的Windows窗体项目的\ bin\Debug文件夹中,现在它可以工作了.Microsoft.Win32.Registry包也发生了确切的情况.所以我几乎确信这是我的错,而且我正在以错误的方式做某事,但是当我使用System.Drawing.Primitive Package进行测试时,它完美无缺,无需复制dll.现在我真的很困惑.
sqlclient ×10
c# ×6
.net ×4
sql-server ×3
asp.net-mvc ×2
sqlcommand ×2
.net-core ×1
asp.net-core ×1
ef-core-7.0 ×1
sql ×1
transactions ×1
vb.net ×1