如何从SQL查询中增加int值 [英] How increase an int value from SQL query

查看:258
本文介绍了如何从SQL查询中增加int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这是我的代码:



Hi everyone,

This is my code:

string SQLquery = "INSERT INTO dbo.ripRiparazioni (ID, Nome, Cognome, IdCliente, Data, Anno, Cellulare, Marca, Modello, NumeroSerie, ProblemaRiscontrato) VALUES (@id,@nome, @cognome, @idcliente,@data, @anno, @cellulare, @marca, @modello, @numeroserie, @problemariscontrato)";

                SqlCommand cmd = new SqlCommand(SQLquery, connessione);

                cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32("SELECT MAX(ID) + 1 FROM dbo.ripRiparazioni;");
                cmd.Parameters.Add("@nome", SqlDbType.VarChar).Value = txtNome.Text.ToUpper();
                cmd.Parameters.Add("@cognome", SqlDbType.VarChar).Value = txtCognome.Text.ToUpper();
                cmd.Parameters.Add("@idcliente", SqlDbType.Int).Value = Convert.ToInt32("SELECT MAX(ID) + 1 FROM dbo.ripRiparazioni;");
                cmd.Parameters.Add("@data", SqlDbType.DateTime).Value = dtpData.Value;
                cmd.Parameters.Add("@anno", SqlDbType.VarChar).Value = txtAnno.Text.ToUpper();
                cmd.Parameters.Add("@cellulare", SqlDbType.VarChar).Value = txtCellulare.Text.ToUpper();
                cmd.Parameters.Add("@marca", SqlDbType.VarChar).Value = txtMarca.Text.ToUpper();
                cmd.Parameters.Add("@modello", SqlDbType.VarChar).Value = txtModello.Text.ToUpper();
                cmd.Parameters.Add("@numeroserie", SqlDbType.VarChar).Value = txtNumeroSerie.Text.ToUpper();
                cmd.Parameters.Add("@problemariscontrato", SqlDbType.Text).Value = rtbProblemaRiscontrato.Text.ToUpper();





我有变量@id和@idcliente,我想选择最大id和为每个新查询增加它(+1)。



我该怎么办?



Where I have the variable @id and @idcliente, I want to select the max id and increase it (+1) for every new query.

How can I do?

推荐答案

你无法将SQL查询转换为整数!就你的C#代码而言,它只是一个strign,它只有在被SQL执行之前没有任何意义。



你可以做到,但你可能想做这是一个存储过程:

1)读取MAX(ID)值。

2)插入所有其他参数,ID + 1和ID + 2适当的栏目。



但为什么?为什么你有两个基本上总是分开的ID值?你可能不会对你插入的下一行的iD使用eh IDCliente值,因为那可能不是同一个用户 - SQL是多用户的,如果你开始玩那样的游戏那么你将会值得所有的痛苦将以PITA的间歇性数据问题的形式出现。



为什么不使用Identity字段为了你的ID,让SQL整理出来的值?
You can't convert an SQL query to an integer! it's just a strign as far as your C# code is concerned, it means nothing until it is executed by SQL.

You can do it, but you probably want to do this as a stored procedure:
1) Read the MAX(ID) value.
2) Insert all the other parameters, and ID+1 and ID+2 in the appropriate columns.

But why? Why have you got two ID values which are basically always going to be one apart? It can't be that you are going to use eh IDCliente value for the iD of the next row you insert, because that might well not be the same user - SQL is multiuser and if you start playing games like that then you are going to deserve all the pain that is going to come your way in the shape of intermittent mangled data problems which are a PITA to sort out.

Why not just use an Identity field for your ID and let SQL sort out the value?


这篇关于如何从SQL查询中增加int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆