C#数据库应用程序,帮忙注释下代码,谢谢
namespaceMessageBoard{publicpartialclassForm1:Form{SqlConnectionconn=newSqlConnection...
namespace MessageBoard
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=.;database=MessageBoard;integrated security=true");//连接数据库
public Form1()
{
InitializeComponent();//初始化窗体
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void AddUser()//绑定下拉列表中的发言用户
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from UserInfo", conn);
adp.Fill(ds, "userInfo");
conn.Close();
cmbUser.DataSource = ds.Tables[0];
cmbUser.DisplayMember = "userName";
cmbUser.ValueMember = "userID"; }
private void Form1_Load(object sender, EventArgs e)
{
AddUser();//调用AddUser类
}
private void btnSubmit_Click(object sender, EventArgs e)
{
conn.Open();
string sql = string.Format("insert into MessageInfo values('{0}',{1})",txtContent.Text,cmbUser.SelectedValue.ToString());
SqlCommand cmd = new SqlCommand(sql, conn);
if (cmd.ExecuteNonQuery() > 0)
{
txtContent.Text = ""; }
conn.Close();
ShowData();//调用方法
}
private void ShowData()
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select b.UserName,a.Contents from MessageInfo as a left join UserInfo as b on a.UserId = b.UserID", conn);
adp.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
}
}
最好加我Q。程序帮我看看。谢谢。。解决了以后追加30分 展开
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=.;database=MessageBoard;integrated security=true");//连接数据库
public Form1()
{
InitializeComponent();//初始化窗体
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void AddUser()//绑定下拉列表中的发言用户
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from UserInfo", conn);
adp.Fill(ds, "userInfo");
conn.Close();
cmbUser.DataSource = ds.Tables[0];
cmbUser.DisplayMember = "userName";
cmbUser.ValueMember = "userID"; }
private void Form1_Load(object sender, EventArgs e)
{
AddUser();//调用AddUser类
}
private void btnSubmit_Click(object sender, EventArgs e)
{
conn.Open();
string sql = string.Format("insert into MessageInfo values('{0}',{1})",txtContent.Text,cmbUser.SelectedValue.ToString());
SqlCommand cmd = new SqlCommand(sql, conn);
if (cmd.ExecuteNonQuery() > 0)
{
txtContent.Text = ""; }
conn.Close();
ShowData();//调用方法
}
private void ShowData()
{
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select b.UserName,a.Contents from MessageInfo as a left join UserInfo as b on a.UserId = b.UserID", conn);
adp.Fill(ds);
conn.Close();
dataGridView1.DataSource = ds.Tables[0];
}
}
}
最好加我Q。程序帮我看看。谢谢。。解决了以后追加30分 展开
2个回答
展开全部
namespace MessageBoard
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=.;database=MessageBoard;integrated security=true");//
连接数据库
public Form1()
{
InitializeComponent();//初始化窗体
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void AddUser()//绑定下拉列表中的发言用户
{
conn.Open();//打开连接
DataSet ds = new DataSet();//创建数据集以存放数据
SqlDataAdapter adp = new SqlDataAdapter("select * from UserInfo", conn);//创建数据适配器,以读取
数据
adp.Fill(ds, "userInfo");//填充数据集
conn.Close();//关闭连接
cmbUser.DataSource = ds.Tables[0];//设置下拉列表的数据源
cmbUser.DisplayMember = "userName";//设置下拉列表显示文本
cmbUser.ValueMember = "userID"; //设置下拉列表的绑定值 }
private void Form1_Load(object sender, EventArgs e)
{
AddUser();//调用AddUser类
}
private void btnSubmit_Click(object sender, EventArgs e)
{
conn.Open();//打开连接
string sql = string.Format("insert into MessageInfo values('{0}',
{1})",txtContent.Text,cmbUser.SelectedValue.ToString());//设置SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);//创建命令对象
if (cmd.ExecuteNonQuery() > 0)//执行SQL语句,且SQL语句影响的行数大于0
{
txtContent.Text = ""; }
conn.Close();//关闭连接
ShowData();//调用方法,显示数据
}
private void ShowData()
{
conn.Open();//打开连接
DataSet ds = new DataSet();//创建数据集以存放数据
SqlDataAdapter adp = new SqlDataAdapter("select b.UserName,a.Contents from MessageInfo as a left
join UserInfo as b on a.UserId = b.UserID", conn);//创建数据适配器,以读取数据
adp.Fill(ds);//填充数据集
conn.Close();//关闭连接
dataGridView1.DataSource = ds.Tables[0];//设置dataGridview的数据源
}
}
}
{
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("server=.;database=MessageBoard;integrated security=true");//
连接数据库
public Form1()
{
InitializeComponent();//初始化窗体
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void AddUser()//绑定下拉列表中的发言用户
{
conn.Open();//打开连接
DataSet ds = new DataSet();//创建数据集以存放数据
SqlDataAdapter adp = new SqlDataAdapter("select * from UserInfo", conn);//创建数据适配器,以读取
数据
adp.Fill(ds, "userInfo");//填充数据集
conn.Close();//关闭连接
cmbUser.DataSource = ds.Tables[0];//设置下拉列表的数据源
cmbUser.DisplayMember = "userName";//设置下拉列表显示文本
cmbUser.ValueMember = "userID"; //设置下拉列表的绑定值 }
private void Form1_Load(object sender, EventArgs e)
{
AddUser();//调用AddUser类
}
private void btnSubmit_Click(object sender, EventArgs e)
{
conn.Open();//打开连接
string sql = string.Format("insert into MessageInfo values('{0}',
{1})",txtContent.Text,cmbUser.SelectedValue.ToString());//设置SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);//创建命令对象
if (cmd.ExecuteNonQuery() > 0)//执行SQL语句,且SQL语句影响的行数大于0
{
txtContent.Text = ""; }
conn.Close();//关闭连接
ShowData();//调用方法,显示数据
}
private void ShowData()
{
conn.Open();//打开连接
DataSet ds = new DataSet();//创建数据集以存放数据
SqlDataAdapter adp = new SqlDataAdapter("select b.UserName,a.Contents from MessageInfo as a left
join UserInfo as b on a.UserId = b.UserID", conn);//创建数据适配器,以读取数据
adp.Fill(ds);//填充数据集
conn.Close();//关闭连接
dataGridView1.DataSource = ds.Tables[0];//设置dataGridview的数据源
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询