Windows程序增删改查

查询代码
public void Bind() {
sqlConnection conn =new sqlConnection(“server=.;database=数据库名称;integrated security=true”);
try{
conn.Open();
string sql =“select * from Student”;
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
DataSet ds =new DataSet();
adapter.Fill(ds);
dgvStudent(控件名称).DataSource = ds.Tables[0];
}
catch(Exception)
throw;
}
finally
{
conn.close();
}
模糊查询SqlConnection conn = new SqlConnection(“Data Source=.;Initial Catalog=FootballScheduleManager;Integrated Security=True”);

        try
        {
            conn.Open();
            string sql = string.Format(@"SELECT s.count,t1.teamName,t2.teamName,s.competitionTime,s.competitionPositon,s.score,s.entryPerson 
                        FROM ScheduleInfo s,TeamInfo t1,TeamInfo t2 WHERE 
                        s.hostTeamIo=t1.id AND s.awayTeamId=t2.id AND t1.teamName LIKE '%{0}%'", txtmc.Text.Trim());
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "ScheduleInfo");
            dgvlie.DataSource = ds.Tables["ScheduleInfo"];
        }
        catch (Exception )
        {

            MessageBox.Show("发生异常!");
        }
        finally {
            conn.Close();
        }

增加代码
sqlConnection conn =new sqlConnection(“server=.;database=数据库名称;integrated security=true”);
try{
conn.Open();
string sql = string.Format(@"INSERT [INTO] 表名 [(列名列表)] VALUES (值列表)改为{0},{1}} ",txtName(控件名称).Text,txtAddress(控件名称).Text );
sqlCommand con =new SqlCommand(sql,conn);
int count =con.ExecuteNonQuery();
if(count>0)
{
Bind();
}else{
MessageBox.Show(“添加失败!”);
}
}
catch(Exception)
throw;
}
finally
{
conn.close();
}

else{
MessageBox.Show(“所有数据都不能为空!”);
}

if(txtAddress(控件名称).Text .Length!=0&&txtName(控件名称).Text.Length!=0){

}

删除代码
DialogResult d = MessageBox.Show(“是否删除”,“提示”,messageBoxButtond.OKCancel);
if(d==DailogResult.OK){
sqlConnection conn =new sqlConnection(“server=.;database=数据库名称;integrated security=true”);
try{
conn.Open();
int id=Convert.ToInt32(dgvStdnt(控件名称).SelectedRows[0].Cells[“id”].Value);
string sql =“delete Student where id=”+id;
sqlCommand con =new SqlCommand(sql,conn);
int count =con.ExecuteNonQuery();
if(count>0)
{
Bind();
}else{
MessageBox.Show(“删除失败!”);
}
}
catch(Exception)
throw;
}
finally
{
conn.close();
}
}

修改代码
public it id;
public string names;
public string address;

int id=Convert.ToInt32(dgvStdnt(控件名称).SelectedRows[0].Cells[“id”].Value);
int names= dgvStdnt(控件名称).SelectedRows[0].Cells[“studentname”].Value.ToString();
int addresss = dgvStdnt(控件名称).SelectedRows[0].Cells[“address”].Value.ToString();
Bind();

txtId.Text=id+"";
txtId.Enabled=false;
txtAddress.Text=address;

sqlConnection conn =new sqlConnection(“server=.;database=数据库名称;integrated security=true”);
try{
conn.Open();

string sql=string.Format(@"UPDATE Students SET 列名=’{0}'where 列名={1} ",txtName.Text);
sqlCommand con =new SqlCommand(sql,conn);
int count =con.ExecuteNonQuery();
if(count>0)
{
this.Close();
}else{
MessageBox.Show(“修改失败!”);
}
}
catch(Exception)
throw;
}
finally
{
conn.close();
}
txtNmae.Text=names;

猜你喜欢

转载自blog.csdn.net/weixin_43558749/article/details/86548714