C# Dapper SQL Server 连接错误:连接字符串中不存在 Dapper 连接到另一个数据库
Posted
技术标签:
【中文标题】C# Dapper SQL Server 连接错误:连接字符串中不存在 Dapper 连接到另一个数据库【英文标题】:C# Dapper SQL Server Connection Error : Dapper Connect to anther database not exist in connection string 【发布时间】:2020-11-25 12:39:16 【问题描述】:我正在使用 Dapper 连接到 SQL server 数据库
我的数据库是 SeasonDB,我创建了另一个名为 SeasonDb2 的数据库来测试修改
在我从 SQL 服务器中删除 SeasonDb2 并将连接字符串更改为 SeasonDB 之前,它一直运行良好
从配置文件中获取连接字符串
<connectionStrings>
<add name="SQLServer_Connection" connectionString="data source=.;integrated security=SSPI;initial catalog=SeasonDB"/>
当我启动应用程序时显示此错误消息
System.Data.DataException
HResult=0x80131501
Message=Error parsing column 1 (DegreeID=1 - Int32)
Source=Dapper
StackTrace:
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3665
at Dapper.SqlMapper.<QueryImpl>d__140`1.MoveNext() in /_/Dapper/SqlMapper.cs:line 1102
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 725
at Season_Models.DataAccess.SQLServerConnector.UsersSearch2(String searchValue) in C:\Users\MohamedSaqr\Source\Repos\PrintAndPublishBranch\SeasonApp\Season Models\DataAccess\SQLServerConnector.cs:line 221
at Season_Models.GlobalData.GlobalData.get_Users() in C:\Users\MohamedSaqr\Source\Repos\PrintAndPublishBranch\SeasonApp\Season Models\GlobalData\GlobalData.cs:line 149
at Season_Application.UI.Login.UserLogin() in C:\Users\MohamedSaqr\Source\Repos\PrintAndPublishBranch\SeasonApp\Season Application\UI\Login.cs:line 100
at Season_Application.UI.Login.SaveOnEnterKeyPressed(Object sender, KeyPressEventArgs e) in C:\Users\MohamedSaqr\Source\Repos\PrintAndPublishBranch\SeasonApp\Season Application\UI\Login.cs:line 170
at System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBox.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 Season_Application.Program.Main() in C:\Users\MohamedSaqr\Source\Repos\PrintAndPublishBranch\SeasonApp\Season Application\Program.cs:line 36
Inner Exception 1:
SqlException: Invalid object name 'SeasonDB2.dbo.TableLastModificationTime'.
运行代码
public List<UserModel> UsersSearch2(string searchValue)
List<UserModel> users;
using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServer_Connection"].ToString()))
var p = new DynamicParameters();
p.Add("@SearchValue", searchValue);
users =
connection.Query<UserModel>("SPGetUsers2", p, commandType: CommandType.StoredProcedure).ToList();
foreach (var user in users)
user.UserDegree = GlobalData.GlobalData.Degrees
.Where(degree => degree.DegreeID == user.DegreeID).ToList()[0];
return users;
错误显示在这里
users =
connection.Query<UserModel>("SPGetUsers2", p, commandType: CommandType.StoredProcedure).ToList();
dapper 连接 SeasonDb2 的问题
SqlException:无效的对象名称“SeasonDB2.dbo.TableLastModificationTime”。
我的存储过程使用 SeasonDB
USE [SeasonDB]
GO
/****** Object: StoredProcedure [dbo].[SPTableLastModificationTime]
Script Date: 25/11/2020 02:43:32 م ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Batch submitted through debugger:
SQLQuery1.sql|7|0|C:\Users\Navy5\AppData\Local\Temp\~vs5FCC.sql
ALTER PROCEDURE [dbo].[SPTableLastModificationTime]
-- Add the parameters for the stored procedure here
@TableName sysname,
@TableLastModificationTime DateTime output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Select @TableLastModificationTime = [TableLastModiticationTime]
FROM [SeasonDB2].[dbo].[TableLastModificationTime]
Where TableName = @TableName
if @TableLastModificationTime is null
Begin
select @TableLastModificationTime =
ius.last_user_update
FROM
sys.dm_db_index_usage_stats ius INNER JOIN
sys.tables tbl ON (tbl.OBJECT_ID = ius.OBJECT_ID)
WHERE ius.database_id = DB_ID() and tbl.name = @TableName
if @TableLastModificationTime is null
Begin
select @TableLastModificationTime = GETDATE()
END
INSERT INTO [dbo].[TableLastModificationTime]
([TableName],TableLastModiticationTime)
VALUES
(@TableName,@TableLastModificationTime)
END
Else
Begin
Declare @TempTime DATETIME
SELECT @TempTime = ius.last_user_update
FROM
sys.dm_db_index_usage_stats ius INNER JOIN
sys.tables tbl ON (tbl.OBJECT_ID = ius.OBJECT_ID)
WHERE ius.database_id = DB_ID() and tbl.name = @TableName
IF @TempTime is not null AND @TempTime <> @TableLastModificationTime
BEGIN
SET @TableLastModificationTime = @TempTime
UPDATE [dbo].[TableLastModificationTime]
SET
[TableLastModiticationTime] = @TableLastModificationTime,
EditLastTime = GetDate()
WHERE [TableName] = @TableName
END
END
END
提前致谢
【问题讨论】:
看来存储过程代码还是引用了SeasonDB2。检查存储过程中的代码 存储过程使用SeasonDB 【参考方案1】:您的问题出在存储过程中。更准确地说,在这里:
Select @TableLastModificationTime = [TableLastModiticationTime]
FROM [SeasonDB2].[dbo].[TableLastModificationTime] ...
SeasonDB2 在查询中被硬编码。
您需要更改您的 SP 并将其删除,因为只要存储过程位于正确的 DB 上,就不需要此前缀
【讨论】:
以上是关于C# Dapper SQL Server 连接错误:连接字符串中不存在 Dapper 连接到另一个数据库的主要内容,如果未能解决你的问题,请参考以下文章
Dapper use Table Value Parameter in C# (Sql Server 数组参数)