zoukankan      html  css  js  c++  java
  • C#与sql server数据库存储过程的操作实例

    通过这几天的学习和实际操作,把C#与sql server数据库存储过程的操作搞清楚了。

     1 using System;
     2 using System.ComponentModel;
     3 using System.Collections.Generic;
     4 using System.Data;
     5 using System.Data.SqlClient;
     6 using System.Drawing;
     7 using System.Linq;
     8 using System.Text;
     9 using System.Windows.Forms;
    10  
    11 namespace sqltest1
    12 {
    13     public partial class Form1 : Form
    14     {
    15         public Form1()
    16         {
    17             InitializeComponent();
    18         }
    19  
    20         private void button1_Click(object sender, EventArgs e)
    21         {
    22             delData(textBox1.Text);
    23         }
    24  
    25         private void delData(string v)
    26         {
    27             SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
    28             conn.Open();
    29             SqlCommand cmd = conn.CreateCommand();
    30             cmd.CommandText = "deldata";
    31             cmd.CommandType = CommandType.StoredProcedure;
    32             //SqlParameter[] sps = new SqlParameter[] { new SqlParameter("@id",v) };
    33             cmd.Parameters.Add(new SqlParameter("@id", v));
    34             int i = cmd.ExecuteNonQuery();
    35             MessageBox.Show($"有{i}条数据受到影响!");
    36         }
    37  
    38         private void button2_Click(object sender, EventArgs e)
    39         {
    40             AddData(textBox2.Text, textBox3.Text);
    41         }
    42  
    43         private void AddData(string text1, string text2)
    44         {
    45             SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
    46             conn.Open();
    47             SqlCommand cmd = conn.CreateCommand();
    48             cmd.CommandText = "AddData";
    49             cmd.CommandType = CommandType.StoredProcedure;
    50             SqlParameter[] sps = new SqlParameter[] {
    51                 new  SqlParameter("@test1",text1),
    52                 new  SqlParameter("@test2",text2)
    53             };
    54             cmd.Parameters.AddRange(sps);
    55             int i = cmd.ExecuteNonQuery();
    56             MessageBox.Show($"有{i}条数据受到影响!");
    57         }
    58     }
    59 }

     
  • 相关阅读:
    Keep it simple & stupid
    BZOJ 2631: tree( LCT )
    BZOJ 2843: 极地旅行社( LCT )
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊( LCT )
    BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )
    BZOJ 3531: [Sdoi2014]旅行( 树链剖分 )
    BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
    BZOJ 2016: [Usaco2010]Chocolate Eating( 二分答案 )
    BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
    BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )
  • 原文地址:https://www.cnblogs.com/heroysj/p/10868137.html
Copyright © 2011-2022 走看看