我定义了一个数据集,然后用一个自定义方法插入数据,部份代码如下:
private int AddMsg()
{
string sErrorMsg="";
int nResult=-1;
DataSet dsMsg = new dsMessage(); DataTable table=dsMsg.Tables["Message"]; DataRow row=new DataRow();
try
{
if(this.txt_Title.Text!="")
{
row["Title"]=this.txt_Title.Text.Trim();
}
else
{
row["Title"]=DBNull.Value;
    }
if(this.txt_Content.Text!="")
{
row["Content"]=this.txt_Content.Text.Trim();
}
else
{
row["Content"]=DBNull.Value;
}
row["Input_Date"]=Convert.ToDateTime(DateTime.Now.ToShortDateString()); table.Rows.Add(row); nResult = bll_Message.InsertMsg(table,out sErrorMsg); }问:在bll_Message类中,InsertMesg(table,out sErrorMsg)方法应该怎么写?
谢谢各位,我是初学者,提的问题可能比较菜.

解决方案 »

  1.   

    1。
    什么数据库?2。
    LZ 得先找书来看看,哪怕翻翻目录逗好
    3。string sql = String.Format("INSERT INTO MyTableName(Title, Content) VALUES('{0}', '{1}')", table.Rows[0]["title"], table.Rows[0]["content"]); // 构造sql 语句SqlConnection conn = new SqlConnection("server=.;database=MyDb;uid=sa;pwd=119911"); // 创建连接对象
    SqlCommand cmd = new SqlCommand(sql, conn); // 创建命令对象
    try {
    conn.Open(); // 打开连接
    cmd.ExecuteNonQuery(); // 执行
    }finally{
    if(conn.State != ConnectionState.Close()) conn.Close(); // 确保关闭连接
    }
    3。
    另外,么发现这里传了一个 DataTable 有什么特别的作为,直接传 title content 不就可以了迈?
      

  2.   

    sorry,if(conn.State != ConnectionState.Close()) conn.Close(); // 确保关闭连接》》》if(conn.State != ConnectionState.Close) conn.Close(); // 确保关闭连接