回答

收藏

检索SQL语句的输出参数

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

我用的表格有两列,customer_id和customer_name。' H5 F; N* Z8 U# _$ w2 Z! M
customer_name简单varchar,customer_id是自动递增的主键。7 m0 h2 I$ V2 V8 B# V; _( \
我想使用C#插入应用程序customer_name,并检索的值customer_id。- K7 e7 Y- x& e% R
只需插入即可,只需使用即可解决/ i( W, F& b6 M$ k6 e
using (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一些语句文档,可用于检索值。9 t# B2 |9 O4 V
我认为正确的语法是) L+ I# y; Y) a: y$ T4 o0 X, p8 f# w
INSERT INTO custom_customer (customer_name) OUTPUT Inserted.customer_id VALUES (@name);我认为我需要使用
% {0 u8 @# R; p4 ?command.ExecuteReader();但后来我被困住了。如何从查询中获得值?
8 y; x) l0 g1 L- ~                                                                ' W$ D: k. V2 @, y  J
    解决方案:                                                               
6 Z. F2 ]' j3 C% g& q' i                                                                首先,使用ExecuteNonQuery写作。执行命令后。Parameters,参数可以从集合中读取,因为参数已经设置为样本的输出参数:7 @! C- E, l( W  R3 G4 p
command.Parameters["name"].Direction = System.Data.ParameterDirection.Output;command.ExecuteNonQuery();object name = command.Parameters["name"].Value;如果想知道Id可使用插入后生成的标志列SCOPE_IDENTITY()sql并使用命令ExecuteScalar()获得命令的结果,如:) H6 F3 O! \0 N! |0 ^
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();
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则