C#怎样通过语句向空的SQL数据表中添加数据!!!

我的 添加窗口如下,代码是
private void button1_Click(object sender, EventArgs e)
{
try
{
string kind = "";
float d = 0.0f;
if (radioButton1.Checked == true)
{ kind = "收入"; }
else
{ kind = "支出"; }
float Loan = new float();
string amount = textBox1.Text.Trim();
string people = textBox2.Text.Trim();
string place = textBox3.Text.Trim();
string explain = textBox4.Text.Trim();
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = "server=PC2010082202NSK\\SQLEXPRESS;initial catalog=FamilyFinance;Integrated Security = SSPI;";
SqlCommand cmd1 = new SqlCommand("select * from FinanceList order by HappenTime", con1);
SqlDataAdapter sda1 = new SqlDataAdapter();
sda1.SelectCommand = cmd1;
DataSet ds1 = new DataSet();
sda1.Fill(ds1, "FL");
// if (ds1.Tables["FL"].Rows[0][0] == null)
//Loan = 0.0f;
Loan = float.Parse(ds1.Tables["FL"].Rows[0][7].ToString()) - float.Parse(ds1.Tables["FL"].Rows[0][3].ToString());
cmd1.CommandText = "insert into FinanceList(ItemKind,HappenTime,Amount,Transactor,FinanceObject,BriefExplain,FamilyAmount) values(\'" + kind + "\',\'" + dateTimePicker1.Text + "\'," + amount + ",\'" + people + "\',\'" + place + "\',\'" + explain + "\',0)";
sda1.InsertCommand = cmd1;
sda1.Fill(ds1, "FL");
con1.Close();
SqlDataAdapter sda2 = new SqlDataAdapter("select * from FinanceList order by HappenTime", con1);
SqlCommandBuilder cmb = new SqlCommandBuilder(sda2);
DataSet ds2 = new DataSet();
sda2.Fill(ds2, "FL");
con1.Close();
DataTable dt = new DataTable();
dt = ds2.Tables["FL"].Copy();
ds2.Tables["FL"].Rows.Clear();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
if (dt.Rows[i][1].ToString() == "收入")
d = 1.0f;
else
d = -1.0f;
dt.Rows[i][7] = Loan + float.Parse(dt.Rows[i][3].ToString()) * d;
Loan = float.Parse(dt.Rows[i][7].ToString());
}
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{ ds2.Tables["FL"].ImportRow(dt.Rows[i]); }
sda2.Update(ds2, "FL");
MessageBox.Show("记帐成功!");
this.Dispose();
}
catch (Exception e2)
{
MessageBox.Show(e2 .Message );
}
}表
}表的格式是这个ItemNum int IDENTITY(1,1) NOT NULL,ItemKind nchar(10) NOT NULL,HappenTime datetime NOT NULL,Amount float NOT NULL,Transactor text NOT NULL,FinanceObject nvarchar(50) NULL,BriefExplain nvarchar(200) NULL,FamilyAmount float NOT NULL,
其中ItemNum是设置自动增长的,我想把他设置程主键,不知道怎么在C#通过语句实现,最关键的问题就是现在添加不了数据。总是提示C#中怎么样通过语句实现向 SQL 新建的空表中添加数据??我的代码如下,添加的时候出现 在位置0处没有任何行。当数据表有一行数据时,可以正常添加。其中两个注释语句我是这样想的::如果ds1.Tables["FL"].Rows[0][0] == null,我就把LOAN设置成0;可是还是不行,,,跪求高手指点

Loan = float.Parse(ds1.Tables["FL"].Rows[0][7].ToString()) - float.Parse(ds1.Tables["FL"].Rows[0][3].ToString());
cmd1.CommandText = "insert into FinanceList(ItemKind,HappenTime,Amount,Transactor,FinanceObject,BriefExplain,FamilyAmount) values(\'" + kind + "\',\'" + dateTimePicker1.Text + "\'," + amount + ",\'" + people + "\',\'" + place + "\',\'" + explain + "\',0)";
sda1.InsertCommand = cmd1;
sda1.Fill(ds1, "FL");

一些设计上的问题我就不说了,反正一个函数有这么多代码,很明显的就不合理。我就说说代码的问题。
1 float。parse这行你都不考虑得到datarow是否有值甚至ds1是否包含table以及table里面有没有数据都没进行判断,假如("select * from FinanceList order by HappenTime", con1); 这个sql查询没有数据那么parse这里就要报错,其后的插入就不会执行了
2 你这么喜欢用fill,有没有弄明白fill最终使用的是selectcommand,而不是insertcommand,即使你前面的执行正确,这里也不会插入,完全可以用sqlcommand的executenonquery这个方法来执行插入语句!!!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-17
你这样,不可以么?
(一般不会在页面cs代码里面处理数据库插入等操作的,最好来个层来处理这个专门的数据处理操作)
第2个回答  2011-08-17
根本不知道你在问什么
第3个回答  2011-08-17
代码写得太乱了,变量命名也不好...
看不懂你在问什么,而且将字段设置为主键,在建表的时候就可以搞定啊
相似回答