简体   繁体   English

C#SQL Server 2008 R2插入存储过程将不起作用

[英]C# sql server 2008 r2 insert stored procedure won't work

i've got this stored procudure that won't work. 我有此存储的程序将无法正常工作。 If I try this query in the management studio with my parameters filled in it works, i can't see if i've done anything wrong in my code but i hope someone here does notice something i'm doing wrong 如果我在Management Studio中尝试使用此参数进行填充的查询,那么我看不到我的代码做错了什么,但我希望这里有人注意到我做错了什么

CREATE PROCEDURE new_project 
-- Add the parameters for the stored procedure here
@proj_naam nvarchar = null,
@plaats nvarchar = null,
@opd_geef int = 0,
@status int = 0,
@proj_id int = 0  AS  BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO project (naam_project, plaats, opdrachtgeverZEEBREGTS_nr, status, project_NR)
VALUES (@proj_naam, @plaats, @opd_geef, @status, @proj_id)  END  GO

and c# code: 和C#代码:

System.Data.SqlClient.SqlConnection con;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = Global.ConnectionString_fileserver;
            con.Open();
            string stopro = "";
            switch (type)
            {
                case 1:
                    stopro = "new_project";
                    break;
                case 2:
                    stopro = "new_bedrijf";
                    break;
                case 3:
                    stopro = "new_persoon";
                    break;
            }
            SqlCommand command = new SqlCommand(stopro, con);
            command.CommandType = CommandType.StoredProcedure;
            switch (type)
            {
                case 1:
                    SqlParameter proj_naam = command.Parameters.Add("@proj_naam", SqlDbType.NVarChar);
                    SqlParameter plaats = command.Parameters.Add("@plaats", SqlDbType.NVarChar);
                    SqlParameter opdrachtgever = command.Parameters.Add("@opd_geef", SqlDbType.Int);
                    SqlParameter status = command.Parameters.Add("@status", SqlDbType.Int);
                    SqlParameter proj_id = command.Parameters.Add("@proj_id", SqlDbType.Int);
                    proj_naam.Value = tb_proj_projectnaam.Text; proj_naam.Direction = ParameterDirection.Input;
                    plaats.Value = tb_proj_plaats.Text; plaats.Direction = ParameterDirection.Input;
                    opdrachtgever.Value = cb_proj_opdrachtgever.SelectedValue; opdrachtgever.Direction = ParameterDirection.Input;
                    status.Value = cb_proj_status.SelectedValue; status.Direction = ParameterDirection.Input;
                    proj_id.Value = id; proj_id.Direction = ParameterDirection.Input;
                    break;  
            }  
            int nwok = command.ExecuteNonQuery();
            con.Close();

So i hope someone can help thnx in advance! 因此,我希望有人可以提前帮助thnx!

You have a break before your int nwok = command.ExecuteNonQuery(); 您需要先break一下int nwok = command.ExecuteNonQuery();

EDIT 编辑

Not related to why your SPROC isn't executing but you've got SET NOCOUNT ON which will cause -1 to be returned by ExecuteNonQuery . 与您的SPROC不执行的原因无关,但是您已将SET NOCOUNT ON ,这将导致ExecuteNonQuery返回-1 Are you running SET NOCOUNT OFF before the insert? 您在插入之前是否正在运行SET NOCOUNT OFF

我可以看到,如果在您的切换案例中Case不为1 ,那么该命令实例将永远无法获取这些参数,因此它将无法正常工作。

Here you are testing the type in a switch statement: 在这里,您要在switch语句中测试type

switch (type)             
{                 
    case 1:                     
        stopro = "new_project";                     
        break;                 
    case 2:
        stopro = "new_bedrijf";                     
        break;
    // etc.
}

You then retest the type in another switch statement which is just overkill. 然后,您在另一个switch语句中重新测试了该type ,不过这是过分的。 Move your parameters into the first switch statement and this may resolve your error. 将参数移到第一个switch语句中,这可能会解决您的错误。

switch (type)             
{                 
    case 1:                     
        stopro = "new_project";     
        SqlParameter proj_naam = command.Parameters.Add("@proj_naam", SqlDbType.NVarChar);                     
        SqlParameter plaats = command.Parameters.Add("@plaats", SqlDbType.NVarChar);
        SqlParameter opdrachtgever = command.Parameters.Add("@opd_geef", SqlDbType.Int); 
        SqlParameter status = command.Parameters.Add("@status", SqlDbType.Int); 
        SqlParameter proj_id = command.Parameters.Add("@proj_id", SqlDbType.Int);
        proj_naam.Value = tb_proj_projectnaam.Text; 
        proj_naam.Direction = ParameterDirection.Input;   
        plaats.Value = tb_proj_plaats.Text; 
        plaats.Direction = ParameterDirection.Input; 
        opdrachtgever.Value = cb_proj_opdrachtgever.SelectedValue; 
        opdrachtgever.Direction = ParameterDirection.Input;
        status.Value = cb_proj_status.SelectedValue; 
        status.Direction = ParameterDirection.Input;
        proj_id.Value = id; 
        proj_id.Direction = ParameterDirection.Input;                    
        break;                 
    case 2:
        stopro = "new_bedrijf";                     
    // etc.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server 2008 R2存储过程不起作用(visual c#) - SQL Server 2008 R2 stored procedure doesn't work (visual c#) 使用C#安装SQL Server 2008 R2 Express - Install SQL Server 2008 R2 Express using C# 具有现有SQL Server 2008 R2数据库的C#应用 - C# App with Existing SQL Server 2008 R2 Database 在MSSQL SERVER 2008 R2上创建动态存储过程 - Creating a dynamic Stored Procedure on MSSQL SERVER 2008 R2 C#代码与SQL Server 2008存储过程的性能比较 - C# code and SQL Server 2008 stored procedure performance comparison 如何使用Windows Service(C#)中的多线程在SQL Server 2008 R2中插入和更新同一插入的多条记录? - How to insert & update the same inserted multiple record in SQL Server 2008 R2 using multithread in windows service(C#)? 想要从VS2012调试SQL Server 2008 R2存储过程-在SQL Server对象资源管理器中看不到数据库 - Want to debug SQL Server 2008 R2 stored procedure from VS2012 - Cannot see database in the SQL Server Object Explorer 存储过程不返回任何数据asp.net sql server 2008 r2 - store procedure don't return any data asp.net sql server 2008 r2 C# & SQL Server 存储过程多次插入动作,影响? - C# & SQL Server stored procedure multiple insert actions, influence? 进行设置以使用C#连接到SQL Server 2008 R2中的服务器 - Make a setting to connect to server in SQL Server 2008 R2 using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM