日期:2014-05-17  浏览次数:20840 次

C#数据库插入数据问题
我现在在用C#编一个软件,我希望每隔1秒向SQL数据库的表中插入一个数据,不知道该怎么做啊
我现在用的代码是
 private void timer3_Tick(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                int temdata = Convert.ToInt32(receive.Text);
                string sql = "insert into TemTable (温度) values (temdata)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();//将连接打开
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "失败");
                }
            }
        }
但是这样不对也,还有人说应该用table比较好,这样不会对数据库频繁的进行操作,但是我不知道怎么用SqlDataAdapter对象来对table进行插入诶。求详细指导
sql table

------解决方案--------------------
        /// <summary>
        /// 将DataSet中的数据一次性存储到数据库
        /// </summary>
        /// <param name="sql">数据</param>
        /// <param name="strTblName">要操作的表</param>
        /// <returns></returns>
public static int CommondDataTable(DataTable dt, string strTblName)
        {
 
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                Connection.Open();
                SqlTransaction trans = Connection.BeginTransaction();
                try
                {
              &n