visual studio c#连接了sql~为什么我的数据没办法写到数据库表

Yellow5A5 2014-04-05 12:41:50
大家可以帮我看看为什么我的数据没办法写到数据库表里面么??

SqlConnection con =new SqlConnection("Data Source=.;Initial Catalog=刷卡记录;Integrated Security=True");
if(con.State==ConnectionState.Closed)
con.Open();
string strsql = "insert into 门禁记录(卡号,时间)value('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand comm = new SqlCommand(strsql, con);
con.Close();

真心求救啊= =!!
...全文
208 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wind_cloud2011 2014-04-05
  • 打赏
  • 举报
回复

 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=刷卡记录;User ID=sa;password=sa;Integrated Security=False");            
            con.Open();
            SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from 门禁记录 where 卡号= '{0}'", textBox1.Text), con);
            if ((int)cmd.ExecuteScalar() > 0)
            {
               listBox1.Items.Add(s1 + " 数据已经存在");
            }
            else
            {

                
                string strsql = "insert into 门禁记录(卡号,时间)value('" + textBox1.Text + "','" + textBox2.Text + "')";
                cmd.CommandText = strsql;
                cmd.ExecuteNonQuery();
               listBox1.Items.Add(s1 + " 成功添加");
            }
            cmd.Dispose();
            con.Close();


黄大仙儿 2014-04-05
  • 打赏
  • 举报
回复
给你个数据库的Helper

        public static DataSet GetResult(string cmd)
        {
                string conn =  "Data Source=BENDAN-PC;Initial Catalog=JMstudent;Integrated Security=True";
                SqlConnection con = new SqlConnection(conn);
                con.Open();
                SqlDataAdapter ada = new SqlDataAdapter(cmd, con);
                DataSet ds = new DataSet();
                ada.Fill(ds);
                return ds;

        }

        public static bool ExecCmd(string cmd)//增加修改删除
        {
            string conn = "Data Source=BENDAN-PC;Initial Catalog=JMstudent;Integrated Security=True";
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            SqlTransaction st = con.BeginTransaction();//启用事务实现
            SqlCommand com = con.CreateCommand();
            com.Transaction = st;
            try
            {
                com.CommandText = cmd;
                com.ExecuteNonQuery();
                st.Commit();
                return true;
            }
            catch
            {
                st.Rollback();
                return false;
            }
        }
Banianer 2014-04-05
  • 打赏
  • 举报
回复
引用 6 楼 u011035622 的回复:
[quote=引用 4 楼 banian_cn 的回复:]
gridview 你不提交数据 怎么能自动跑到数据库里去? 你得有提交的事件。


有提交啊~这些代码是放在一个button里面的啊~我没把它贴出来而已[/quote]
你断点调试看数据是否正确。 你的SQL语句是否是你预期的数据。
Banianer 2014-04-05
  • 打赏
  • 举报
回复
引用 6 楼 u011035622 的回复:
[quote=引用 4 楼 banian_cn 的回复:]
gridview 你不提交数据 怎么能自动跑到数据库里去? 你得有提交的事件。


有提交啊~这些代码是放在一个button里面的啊~我没把它贴出来而已[/quote]
你断点调试看数据是否正确。 你的SQL语句是否是你预期的数据。
Yellow5A5 2014-04-05
  • 打赏
  • 举报
回复
引用 4 楼 banian_cn 的回复:
gridview 你不提交数据 怎么能自动跑到数据库里去? 你得有提交的事件。
有提交啊~这些代码是放在一个button里面的啊~我没把它贴出来而已
Banianer 2014-04-05
  • 打赏
  • 举报
回复
gridview 你不提交数据 怎么能自动跑到数据库里去? 你得有提交的事件。
Yellow5A5 2014-04-05
  • 打赏
  • 举报
回复
啊~我后面加入了、 SqlConnection con =new SqlConnection("Data Source=.;Initial Catalog=刷卡记录;Integrated Security=True"); if(con.State==ConnectionState.Closed) con.Open(); string strsql = "insert into 门禁记录(卡号,时间)value('" + textBox1.Text + "','" + textBox2.Text + "')"; SqlCommand comm = new SqlCommand(strsql, con); comm.ExecuteNonQuery(); con.Close(); 可以往gridview里写东西了。但是写的东西只会存储在gridview里面~~不会传到数据库的表里面。 ( 在数据库表里面写东西,可以传到gridview里面。。反过来却不行)
Banianer 2014-04-05
  • 打赏
  • 举报
回复
你都没执行 就 con.Close(); 先执行 再关闭。
Banianer 2014-04-05
  • 打赏
  • 举报
回复
comm.ExecuteNonQuery();

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧