Beginning C# 2008 Databases From Novice to Professional phần 6

Bạn sau đó thêm hai tham số, @ fname và @ lname, để sở hữu bộ sưu tập tham số của lệnh bạn muốn tham số. Hãy CommandParameters các dự án khởi động, và sau đó chạy nó bằng cách nhấn Ctrl + F5. Bạn sẽ thấy kết quả trong hình 11-7. | CHAPTER 11 EXECUTING COMMANDS 231 After DELETE Number of employees 0 n catch SqlException ex finally Connection Closed. 3. Make Commandparameters the startup project and then run it by pressing Ctrl F5. You should see the results in Figure 11-7. Figure 11-7. Using command parameters 232 CHAPTER 11 EXECUTING COMMANDS How It Works First you set up your sample data. set up rudimentary data string fname Zachariah string Iname Zinn You then add two parameters @fname and @lname to the Parameters collection property of the command you want to parameterize. create commands SqlCommand cmdqry new SqlCommand sqlqry conn SqlCommand cmdnon new SqlCommand sqlins conn add parameters to the command for statements @fname 10 @lname 20 Note that you provide the parameter names as strings and then specify the data types of the columns you expect to use them with. The SqlDbType enumeration contains a member for every SQL Server data type except cursor and table which can t be directly used by C programs. The Add method is overloaded. Since nvarchar requires you to specify its maximum length you include that as the third argument. Finally you set the parameter values before executing the command. execute nonquery to insert an employee @fname .Value fname @lname .Value lname Note You use the same command cmdnon to execute both the INSERT and DELETE statements. The parameter values don t change even though the SQL in CommandText does. The Parameters collection is the source of parameter values for whatever SQL is in CommandText. The SQL does not have to use all or even any of the parameters but it cannot use any parameters not in the command s Parameters collection. Notice in Figure 11-7 that when you display the SQL in CommandText you see the parameter names .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
8    320    1    12-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.