C#2012连接SQL server2008设计机票预订系统
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 航空机票预订管理系统
{
public partial class ticket : Form
{
LinkDatabase link = new LinkDatabase();
public ticket()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
string sendstrsql = "SELECT 航班号,座位号,机票类型 FROM 机票 WHERE 航班号='+ sel_Pno.Text +' AND 座位号='+ sel_Pseat.Text +'";
DataTable dt1 = link.dsresult(sendstrsql);
dataGridView2.DataSource= dt1.DefaultView;
}
private bool checkEmpty()
{
bool result = true;
if (add_Pseat.Text.Trim() == string.Empty)
result = false;
else if (add_Tno.Text.Trim() == string.Empty)
result = false;
else if (add_Ttype.Text.Trim() == string.Empty)
result = false;
return result;
}
private void button1_Click(object sender, EventArgs e)
{
if (checkEmpty() == true)
{
try
{
string sendstrsql = "INSERT INTO 机票 VALUES('" + add_Pseat.Text + "','" + add_Tno + "','" + add_Ttype + "')";
link.updatedb(sendstrsql);
MessageBox.Show("成功添加数据");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else MessageBox.Show("输入不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string sendstrsql = "DELETE FROM 机票 WHERE 座位号=" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "AND 座位号=" + Convert.ToString(dataGridView1[2,dataGridView1.CurrentCell.RowIndex].Value).Trim() + "";
link.updatedb(sendstrsql);
MessageBox.Show("记录删除成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
查询之后,可以出来表格但是并未能显示数据!!!请问怎样修改代码?!!!我已经在dataGridView控件的DataSource属性里添加了数据库表。。。
[最优解释]
string sendstrsql = "SELECT 航班号,座位号,机票类型 FROM 机票 WHERE 航班号='"+ sel_Pno.Text +"' AND 座位号='"+ sel_Pseat.Text +"'";
[其他解释]
把sql语句放入LIST集合中,然后就是直接将list赋给datagridview
[其他解释]
list集合?!请问是什么 在sql里面么
[其他解释]
用存储过程或参数化处理比较好 楼主的这个会被注入
------其他解决方案--------------------
3楼的正解