文档库 最新最全的文档下载
当前位置:文档库 › 增删改查参考代码

增删改查参考代码

删除信息参考代码:
//取得文本框中的信息
string bookName = this.textBox1.Text.Trim();
//T-SQL查询(选择表查询出相关字段删除)
string sql = "delete booksinfo where bookName = '"+bookName+"'";
//定义变量count
int count = 0;
//
try
{
//连接数据库
SqlConnection conn = new SqlConnection("server=.;database=bookinfo;uid=sa;pwd=;");
//打开数据库
conn.Open();
//执行语句选择对象
SqlCommand com = new SqlCommand(sql, conn);
//显示受影响的数量(ExecuteNonQuery只能用于增删改不用于查询)
count = com.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

if (count > 0)
{
MessageBox.Show("delete ok");
}
else
{
MessageBox.Show("delete error");
}

增加信息参考代码
string bookName = this.textBox1.Text.Trim();
string price = this.textBox2.Text.Trim();
DateTime pubDate = this.dateTimePicker1.Value;

int y = pubDate.Year;
int m = pubDate.Month;
int d = pubDate.Day;

string newDate = y + "-" + m + "-" + d;


string author = this.textBox3.Text.Trim();
string type = https://www.wendangku.net/doc/a05185579.html,boBox1.Text;

if (bookName.Equals(""))
{
MessageBox.Show("bookName is null!");
return;
}

if (price.Equals(""))
{
MessageBox.Show("price is null!");
return;
}

string sql = "insert into BooksInfo (BookName,Price,PubDate,Author,Type) values ('" + bookName + "'," + price + ",'" + newDate + "','" + author + "'," + type + ")";

int count = 0;
try
{
SqlConnection conn = new SqlConnection("server=.;database=bookinfo;uid=sa;pwd=;");
conn.Open();

SqlCommand com = new SqlCommand(sql, conn);
count = com.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

if (count > 0)
{
MessageBox.Show("insert ok");
}
else
{
MessageBox.Show("insert error");
}

查询信息参考代码
//取得文本框用户名的数据
string userName = UserTxet.Text.Trim();
//取得文本框密码的数据
string

userPwd = PwdText.Text.Trim();
//判断用户名,密码不能为空,提示用户输入
if (userName == "")
{
MessageBox.Show("用户名不能为空!", "温馨提示!");
return;
}
if (userPwd == "")
{
MessageBox.Show("密码不能为空!", "温馨提示!");
return;
}
//T-SQL查询(选择用户表查询的列名)
string sql = "select count(*) from User where UserName = '" + userName + "' and UserPwd = '" + userPwd + "'";
//定义一个变量
int rowCount = 0;
//TRY块显示出错信息
try
{
//连接连接数据
string connString = "Data Source ='何银财\\SQLEXPRESS'; Initial Catalog = BookDb; User Id = 'sa'; Pwd = '0401'";
//连接数据库字符串
SqlConnection conn = new SqlConnection(connString);
//打开数据库连接
conn.Open();
//Command执行语句 执行SQL查询和数据库连接
SqlCommand com = new SqlCommand(sql, conn);
//收集查询到的行数
rowCount = Convert.ToInt32(com.ExecuteScalar());
//关闭数据库连接
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
//判断收集的行数是否大于0,如果大于零提示登陆成功。小于零提示错误

if (rowCount == 0)
{
MessageBox.Show("你不是系统的有效用户!","登陆出错!");
}
else
{
MessageBox.Show("恭喜登陆成功!");
}


窗体事件加载查询
private void FormAddBookInfo_Load(object sender, EventArgs e)
{
string sql = "select * from TypeInfo";

SqlConnection conn = new SqlConnection("server=.;database=bookinfo;uid=sa;pwd=;");
conn.Open();
SqlCommand com = new SqlCommand(sql,conn);

SqlDataReader reader = com.ExecuteReader();

while(reader.Read())
{
string typeID = reader[0].ToString();

https://www.wendangku.net/doc/a05185579.html,boBox1.Items.Add(typeID);
}

reader.Close();

conn.Close();

}

相关文档
相关文档 最新文档