using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace NewsManagerSys
{
public partial class FrmMain : Form
{
DataSet ds = new DataSet();
static string strSql = "Data Source=.;Initial Catalog=NewsManagerSys;Integrated Security=True";
SqlConnection conn = new SqlConnection(strSql); public FrmMain()
{
InitializeComponent();
} //窗体加载
private void FrmMain_Load(object sender, EventArgs e)
{
Quanbu(); //窗体运行显示全部新闻信息
} //窗体运行显示全部新闻信息
#region 填充Fill
private void Quanbu()
{
if (ds.Tables["News"] != null)
{
ds.Tables["News"].Clear();
}
string str = "select Id,title,begintime,author from News order by id desc";
DataSet result = DBHelper.DataMuster(str, "News");
dgvNews.DataSource = result.Tables["News"];
//try
//{
// SqlDataAdapter sda = new SqlDataAdapter("select Id,title,begintime,author from News order by id desc", conn); // //try
// //{
// sda.Fill(ds, "News"); //} //}
//catch (Exception ex)
//{ // MessageBox.Show(ex.Message);
//}
//this.dgvNews.DataSource = ds.Tables["News"];
}
#endregion
private void dgvNews_SelectionChanged(object sender, EventArgs e)
{
if (this.dgvNews.SelectedRows.Count > 0)
{
this.txtTitle.Text = dgvNews.CurrentRow.Cells["title"].Value.ToString();
this.txtbegintime.Text = dgvNews.CurrentRow.Cells["begintime"].Value.ToString();
this.txtauthor.Text = dgvNews.CurrentRow.Cells["author"].Value.ToString(); //新闻内容
try
{
conn.Open(); //打开数据库连接 int id = Convert.ToInt32(this.dgvNews.SelectedRows[0].Cells["Id"].Value); string sql = string.Format("select * from News where id={0}",id);
SqlCommand comm = new SqlCommand(sql,conn);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
this.txtContent.Text = reader["content"].ToString();
}
reader.Close();//已关
}
catch (Exception)
{
MessageBox.Show("数据连接失败!");
}
}
}
//增加信息
private void btnzhengjia_Click(object sender, EventArgs e)
{
if ((this.txtTitle.Text == string.Empty) ||(this.txtContent.Text == string.Empty) || (this.txtauthor.Text == string.Empty))
{
MessageBox.Show("新闻标题,内容和作者均不能为空!", "提示", MessageBoxButtons.OK);
}
else
{
NewsInsert(); //新增新闻信息
Quanbu();
}
} //新增新闻信息
private void NewsInsert()
{
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open(); //打开数据库连接
}
string sql = string.Format("insert News(title,content,begintime,author)values('{0}','{1}',default,'{2}')", this.txtTitle.Text, this.txtContent.Text, this.txtauthor.Text);
SqlCommand comm = new SqlCommand(sql, conn);
int i = comm.ExecuteNonQuery();
if (i == 1)
{
MessageBox.Show("录入成功!");
Quanbu(); //窗体运行显示全部新闻信息
}
else
{
MessageBox.Show("录入失败!");
} } catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally
{
conn.Close(); //关闭数据库连接
}
} //执行关闭按钮
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否要退出?","提示",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit(); //退出当前窗体
}
} private void dgvNews_SelectionChanged_1(object sender, EventArgs e)
{
if (this.dgvNews.SelectedRows.Count > 0)
{
this.txtTitle.Text = dgvNews.CurrentRow.Cells["title"].Value.ToString();
this.txtbegintime.Text = dgvNews.CurrentRow.Cells["begintime"].Value.ToString();
this.txtauthor.Text = dgvNews.CurrentRow.Cells["author"].Value.ToString(); //新闻内容 conn.Open(); //打开数据库连接 int id = Convert.ToInt32(this.dgvNews.SelectedRows[0].Cells["Id"].Value); string sql = string.Format("select * from News where id={0}", id);
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
this.txtContent.Text = reader["content"].ToString();
}
reader.Close();
}
}
}
}标注颜色的地方是出错的地方:问题
首先窗体加载查询所有的信息显示到datagrideview上,单击增加提示录入成功后,需要刷新datagrideview 就在刷新的时间提示“连接未关闭 连接的状态为打开状态”
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace NewsManagerSys
{
public partial class FrmMain : Form
{
DataSet ds = new DataSet();
static string strSql = "Data Source=.;Initial Catalog=NewsManagerSys;Integrated Security=True";
SqlConnection conn = new SqlConnection(strSql); public FrmMain()
{
InitializeComponent();
} //窗体加载
private void FrmMain_Load(object sender, EventArgs e)
{
Quanbu(); //窗体运行显示全部新闻信息
} //窗体运行显示全部新闻信息
#region 填充Fill
private void Quanbu()
{
if (ds.Tables["News"] != null)
{
ds.Tables["News"].Clear();
}
string str = "select Id,title,begintime,author from News order by id desc";
DataSet result = DBHelper.DataMuster(str, "News");
dgvNews.DataSource = result.Tables["News"];
//try
//{
// SqlDataAdapter sda = new SqlDataAdapter("select Id,title,begintime,author from News order by id desc", conn); // //try
// //{
// sda.Fill(ds, "News"); //} //}
//catch (Exception ex)
//{ // MessageBox.Show(ex.Message);
//}
//this.dgvNews.DataSource = ds.Tables["News"];
}
#endregion
private void dgvNews_SelectionChanged(object sender, EventArgs e)
{
if (this.dgvNews.SelectedRows.Count > 0)
{
this.txtTitle.Text = dgvNews.CurrentRow.Cells["title"].Value.ToString();
this.txtbegintime.Text = dgvNews.CurrentRow.Cells["begintime"].Value.ToString();
this.txtauthor.Text = dgvNews.CurrentRow.Cells["author"].Value.ToString(); //新闻内容
try
{
conn.Open(); //打开数据库连接 int id = Convert.ToInt32(this.dgvNews.SelectedRows[0].Cells["Id"].Value); string sql = string.Format("select * from News where id={0}",id);
SqlCommand comm = new SqlCommand(sql,conn);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
while(reader.Read())
{
this.txtContent.Text = reader["content"].ToString();
}
reader.Close();//已关
}
catch (Exception)
{
MessageBox.Show("数据连接失败!");
}
}
}
//增加信息
private void btnzhengjia_Click(object sender, EventArgs e)
{
if ((this.txtTitle.Text == string.Empty) ||(this.txtContent.Text == string.Empty) || (this.txtauthor.Text == string.Empty))
{
MessageBox.Show("新闻标题,内容和作者均不能为空!", "提示", MessageBoxButtons.OK);
}
else
{
NewsInsert(); //新增新闻信息
Quanbu();
}
} //新增新闻信息
private void NewsInsert()
{
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open(); //打开数据库连接
}
string sql = string.Format("insert News(title,content,begintime,author)values('{0}','{1}',default,'{2}')", this.txtTitle.Text, this.txtContent.Text, this.txtauthor.Text);
SqlCommand comm = new SqlCommand(sql, conn);
int i = comm.ExecuteNonQuery();
if (i == 1)
{
MessageBox.Show("录入成功!");
Quanbu(); //窗体运行显示全部新闻信息
}
else
{
MessageBox.Show("录入失败!");
} } catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally
{
conn.Close(); //关闭数据库连接
}
} //执行关闭按钮
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("是否要退出?","提示",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit(); //退出当前窗体
}
} private void dgvNews_SelectionChanged_1(object sender, EventArgs e)
{
if (this.dgvNews.SelectedRows.Count > 0)
{
this.txtTitle.Text = dgvNews.CurrentRow.Cells["title"].Value.ToString();
this.txtbegintime.Text = dgvNews.CurrentRow.Cells["begintime"].Value.ToString();
this.txtauthor.Text = dgvNews.CurrentRow.Cells["author"].Value.ToString(); //新闻内容 conn.Open(); //打开数据库连接 int id = Convert.ToInt32(this.dgvNews.SelectedRows[0].Cells["Id"].Value); string sql = string.Format("select * from News where id={0}", id);
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
while (reader.Read())
{
this.txtContent.Text = reader["content"].ToString();
}
reader.Close();
}
}
}
}标注颜色的地方是出错的地方:问题
首先窗体加载查询所有的信息显示到datagrideview上,单击增加提示录入成功后,需要刷新datagrideview 就在刷新的时间提示“连接未关闭 连接的状态为打开状态”
{
conn.Close(); //关闭数据库连接
MessageBox.Show("录入成功!");
Quanbu(); //窗体运行显示全部新闻信息
}