回答

收藏

检索SQL语句的输出参数

技术问答 技术问答 109 人阅读 | 0 人回复 | 2023-09-12

我用的表格有两列,customer_id和customer_name。6 d6 k# K* W7 }- A" P, I9 V
customer_name简单varchar,customer_id是自动递增的主键。9 o" K" W& q, l2 ]& ^+ U
我想使用C#插入应用程序customer_name,并检索的值customer_id。
4 a) [& h8 ?' W# }1 I+ G* T" C只需插入即可,只需使用即可解决
7 p7 J( v! J! n  `; y  y. husing (SqlConnection connection = new SqlConnection(AppConstants.ConnectionString)){    using (SqlCommand command = new SqlCommand("INSERT INTO custom_customer (customer_name) VALUES (@name);"))                                                                                                                                                                                                                 command.Parameters.Add(new SqlParameter("name",customer));        connection.Open();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;command.ExecuteNonQuery();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;connection.Close()我找到了该OUTPUT一些语句文档,可用于检索值。
2 W2 k9 i  R( V6 D) o5 ^: D( z- p我认为正确的语法是( o, Z! O3 e! U2 G- t
INSERT INTO custom_customer (customer_name) OUTPUT Inserted.customer_id VALUES (@name);我认为我需要使用4 }6 ?8 [$ }& ?0 |
command.ExecuteReader();但后来我被困住了。如何从查询中获得值?$ T" N7 ~& c- O% K" A+ F
                                                                & F* y1 `7 C* p0 v# b
    解决方案:                                                               
' p/ t9 e: x* w+ z                                                                首先,使用ExecuteNonQuery写作。执行命令后。Parameters,参数可以从集合中读取,因为参数已经设置为样本的输出参数:8 h$ O% E0 |2 K$ Q% i
command.Parameters["name"].Direction = System.Data.ParameterDirection.Output;command.ExecuteNonQuery();object name = command.Parameters["name"].Value;如果想知道Id可使用插入后生成的标志列SCOPE_IDENTITY()sql并使用命令ExecuteScalar()获得命令的结果,如:9 R+ J: w$ _  q  b2 P" i
int id;using (SqlConnection connection = new SqlConnection(AppConstants.ConnectionString)){    string sql = @"INSERT INTO custom_customer (customer_name)                                         VALUES (@name);                    SELECT SCOPE_IDENTITY();"    using (SqlCommand command = new SqlCommand(sql))                                                                                                                                                                                                                 command.Parameters.Add(new SqlParameter("name",customer));        connection.Open();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;id = (int) command.ExecuteScalar();;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;connection.Close();
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则