C#应用程序中的datagridview [英] datagridview in c# application

查看:64
本文介绍了C#应用程序中的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从Datagridview更新到数据库.我做了以下编码.但是它给出了错误.我该如何解决这个prb.pls帮助我

i have to update from Datagridview to database.i did with following codings.but it is giving error.how can i solve this prb.pls help me

for (int i = 0; i <= count-1; i++)
            {              
                              
                fname = dataGridView1.Rows[i].Cells["Fees_name"].Value.ToString();
                amt = dataGridView1.Rows[i].Cells["Amount"].Value.ToString();
                string qu = "insert into feesstudcture(Fees_name,Amount)values(''" + fname + "'',''" + amt + "'')";
                string constring = "data source=localhost;database=newschool;userid=root;password=";
                MySqlConnection con = new MySqlConnection(constring);
                con.Open();
                MySqlCommand cmd = new MySqlCommand(qu, con);
                cmd.ExecuteNonQuery();
               
            }
            MessageBox.Show("succes");

推荐答案

您可以尝试...

for(int i = 0; i& lt; = dataGridView1.Rows.Count-1; i ++)
{

fname = dataGridView1.Rows [i] .Cells ["Fees_name"].Value.ToString();
amt = dataGridView1.Rows [i] .Cells ["Amount"].Value.ToString();
字符串qu =插入feestudcture(Fees_name,Amount)values(""+ fname +"'',"+ amt +");
字符串constring =数据源=本地主机;数据库= newschool;用户ID =根;密码=";
MySqlConnection con =新的MySqlConnection(constring);
con.Open();
MySqlCommand cmd =新的MySqlCommand(qu,con);
cmd.ExecuteNonQuery();

}
MessageBox.Show("succes");
You can try this...

for (int i = 0; i &lt;= dataGridView1.Rows.Count-1; i++)
{

fname = dataGridView1.Rows[i].Cells["Fees_name"].Value.ToString();
amt = dataGridView1.Rows[i].Cells["Amount"].Value.ToString();
string qu = "insert into feesstudcture(Fees_name,Amount)values(''" + fname + "''," + amt + ")";
string constring = "data source=localhost;database=newschool;userid=root;password=";
MySqlConnection con = new MySqlConnection(constring);
con.Open();
MySqlCommand cmd = new MySqlCommand(qu, con);
cmd.ExecuteNonQuery();

}
MessageBox.Show("succes");


请尝试....不确定

将循环更改为for(int i = 0; i< count-1> ; i ++)



将Cells ["name"]更改为Cells [indexvalue]例如: Cells [0]



在连接字符串中插入" Integrated Security = false "

希望它能对您有所帮助
Just try.... Not sure

Change loop to for(int i=0;i<count-1>;i++)

or

Change Cells["name"] to Cells[indexvalue] Eg: Cells[0]

or

Insert "Integrated Security=false" in connection string

Hope it will help you


如果有人不知道自己遇到了什么错误,我认为不会有太大帮助.

但是只是看着它.表中的数据类型是您的金额字段..我看到您正在尝试将其作为字符串插入!

您的连接字符串格式正确吗?
i dont think one can be of much help if one doesnt know what error u r getting.

But just by looking at it. what datatype id ur amount field in teh table.. i see ur trying to insert itas a string!

IS ur connection string in correct form?


这篇关于C#应用程序中的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆