簡體   English   中英

C# Dapper SQL 服務器連接錯誤:連接字符串中不存在 Dapper 連接到另一個數據庫

[英]C# Dapper SQL Server Connection Error : Dapper Connect to anther database not exist in connection string

我正在使用 Dapper 連接到 SQL 服務器數據庫

我的數據庫是 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:無效的 object 名稱“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

提前致謝

您的問題出在存儲過程中。 更准確地說,這里:

Select  @TableLastModificationTime =  [TableLastModiticationTime]
    FROM [SeasonDB2].[dbo].[TableLastModificationTime] ...

SeasonDB2在查詢上是硬編碼的。

您需要更改您的 SP 並將其刪除,因為只要存儲過程位於正確的數據庫上,就不需要此前綴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM