簡體   English   中英

為什么我的SQL Server數據庫不使用過程保存數據-C#

[英]Why does my SQL Server database do not save the data using a procedure - C#

我正在嘗試創建一個過程,並在需要插入一些數據時調用它。

當我執行該應用程序並單擊“保存”時,該應用程序至少有15秒鍾沒有響應,然后才能恢復正常。 但是數據沒有保存。

我做了一個測試程序和一些測試功能來測試它。

連接字符串:

public static string Cn = 
       "Data Source = THIAGO\\SERVIDORSQL; Initial Catalog = DBClinica; Integrated Security = true";

插入測試數據的數據層代碼:

public string Adicionar_Teste(Dados_Pacientes dados_Pacientes)
{
    string resp = "";

    SqlConnection sqlConnection = new SqlConnection();

    try
    {
        sqlConnection.ConnectionString = Conexao.Cn;//cn is the SQL DATABASE path
        sqlConnection.Open();

        // SQL command
        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.Connection = sqlConnection;
        sqlCmd.CommandText = "ProcAdicionar_Teste";
        sqlCmd.CommandType = CommandType.StoredProcedure;

        SqlParameter parTeste = new SqlParameter();
        parTeste.ParameterName = "@Teste";
        parTeste.SqlDbType = SqlDbType.VarChar;
        parTeste.Size = 10;
        parTeste.Value = dados_Pacientes.Nome;
        sqlCmd.Parameters.Add(parTeste);
    }
    catch (Exception ex)
    {
        return resp = ex.Message;
    }
    finally
    {
        if (sqlConnection.State == ConnectionState.Open) 
            sqlConnection.Close();
    }

    return resp;
}

業務層:

public static string AdicionarTeste(string teste)
{
    Dados_Pacientes dados = new Dados_Pacientes();
    dados.Nome = teste;

    return dados.Adicionar_Teste(dados);
}

用戶單擊“保存”時調用的form方法:

private void AdicionarTeste()
{
    string x = TextBoxNomeDoPaciente.Text;
    Negocios_Pacientes.AdicionarTeste(x);
}

SQL Server名稱和路徑:

SQL Server數據庫結構

存儲過程:

ALTER PROCEDURE [dbo].[procAdicionar_Teste]
    @Teste VARCHAR(10)
AS
    INSERT INTO Teste (Teste) 
    VALUES (@Teste)

您不執行命令。 您需要在try塊的末尾添加以下語句。

sqlCmd.ExecuteNonQuery();

暫無
暫無

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

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