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.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string str1 = "Data Source=.;Initial Catalog=aaa;Integrated Security=True";
private void Form1_Load(object sender, EventArgs e)
{
}
public bool control_2() //全局方法
{
string str1 = "Data Source=.;Initial Catalog=aaa;Integrated Security=True";
String sql = "select name from sysobjects where Xtype='p' and name='p13'";
SqlConnection a = new SqlConnection(str1);
SqlCommand com= new SqlCommand(sql, a);
a.Open();
SqlDataReader i = com.ExecuteReader();
if (i.HasRows)
{
return true;
}
else
{
return false;
}
a.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (control_2()==true)
{
SqlConnection a = new SqlConnection(str1);
a.Open();
string sql = "p13";
SqlCommand com1 = new SqlCommand(sql, a);
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add("@StuId",SqlDbType.Int).Value=textBox1.Text;
com1.Parameters.Add("@StuName", SqlDbType.NChar,10).Value = textBox2.Text;
com1.ExecuteNonQuery();
a.Close();
}
else
{
SqlConnection a = new SqlConnection(str1);
a.Open();
string sql = "create proc p13 @StuId int,@StuName nchar(10) as insert into Student(StuId,StuName) values(@StuId,@StuName)";
SqlCommand com = new SqlCommand(sql, a);
com.ExecuteNonQuery();
a.Close();
button1_Click(sender,e);
}
}
}
}
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.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string str1 = "Data Source=.;Initial Catalog=aaa;Integrated Security=True";
private void Form1_Load(object sender, EventArgs e)
{
}
public bool control_2() //全局方法
{
string str1 = "Data Source=.;Initial Catalog=aaa;Integrated Security=True";
String sql = "select name from sysobjects where Xtype='p' and name='p13'";
SqlConnection a = new SqlConnection(str1);
SqlCommand com= new SqlCommand(sql, a);
a.Open();
SqlDataReader i = com.ExecuteReader();
if (i.HasRows)
{
return true;
}
else
{
return false;
}
a.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (control_2()==true)
{
SqlConnection a = new SqlConnection(str1);
a.Open();
string sql = "p13";
SqlCommand com1 = new SqlCommand(sql, a);
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add("@StuId",SqlDbType.Int).Value=textBox1.Text;
com1.Parameters.Add("@StuName", SqlDbType.NChar,10).Value = textBox2.Text;
com1.ExecuteNonQuery();
a.Close();
}
else
{
SqlConnection a = new SqlConnection(str1);
a.Open();
string sql = "create proc p13 @StuId int,@StuName nchar(10) as insert into Student(StuId,StuName) values(@StuId,@StuName)";
SqlCommand com = new SqlCommand(sql, a);
com.ExecuteNonQuery();
a.Close();
button1_Click(sender,e);
}
}
}
}