string strConn = "Data Source=.;Initial Catalog=PB_NewsSystem;Integrated Security=true";//创建数据库连接对象 SqlConnection conn = new SqlConnection(strConn);//建立SqlConnection数据库和应用的桥梁 try { conn.Open();//打开数据库 SqlCommand cmd = new SqlCommand();////执行sql语句 和connection对象 cmd.Connection = conn;//创建连接 string sqlStr = "insert into [News] ([NewsTitle],[NewsContent]) values (@NewsTitle,@NewsContent)";//执行sql语句 cmd.CommandText = sqlStr;//代表执行的是SQL语句 cmd.Parameters.Add(new SqlParameter("@NewsTitle", this.txtTitle.Text)); cmd.Parameters.Add(new SqlParameter("@NewsContent", this.txtContent.Text)); cmd.ExecuteNonQuery();//执行向数据库插入一条数据 this.lblMsg.Text = "添加成功!"; } catch { this.lblMsg.Text = "添加失败!"; } finally { conn.Close(); }