通过这几天的学习和实际操作,把C#与sql server数据库存储过程的操作搞清楚了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace sqltest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delData(textBox1.Text);
}
private void delData(string v)
{
SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "deldata";
cmd.CommandType = CommandType.StoredProcedure;
//SqlParameter[] sps = new SqlParameter[] { new SqlParameter("@id",v) };
cmd.Parameters.Add(new SqlParameter("@id", v));
int i = cmd.ExecuteNonQuery();
MessageBox.Show($"有{i}条数据受到影响!");
}
private void button2_Click(object sender, EventArgs e)
{
AddData(textBox2.Text, textBox3.Text);
}
private void AddData(string text1, string text2)
{
SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "AddData";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] sps = new SqlParameter[] {
new SqlParameter("@test1",text1),
new SqlParameter("@test2",text2)
};
cmd.Parameters.AddRange(sps);
int i = cmd.ExecuteNonQuery();
MessageBox.Show($"有{i}条数据受到影响!");
}
}
}
原文地址:https://www.cnblogs.com/heroysj/p/10868137.html