C#怎样通过语句向空的SQL数据表中添加数据!!!
我的添加窗口如下,代码是privatevoidbutton1_Click(objectsender,EventArgse){try{stringkind="";float...
我的 添加窗口如下,代码是
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 );
}
}表 展开
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 );
}
}表 展开
2个回答
展开全部
float Loan = new float();
string amount = textBox1.Text.Trim();
string people = textBox2.Text.Trim();
string place = textBox3.Text.Trim();
string explain = textBox4.Text.Trim();
修改为:
float Loan = new float();
string amount = ‘’;
string people = ‘’;
string place = ‘’;
string explain = ;
amount = textBox1.Text.Trim();
people = textBox2.Text.Trim();
place = textBox3.Text.Trim();
explain = textBox4.Text.Trim();
先定义变量再赋值就好了。
string amount = textBox1.Text.Trim();
string people = textBox2.Text.Trim();
string place = textBox3.Text.Trim();
string explain = textBox4.Text.Trim();
修改为:
float Loan = new float();
string amount = ‘’;
string people = ‘’;
string place = ‘’;
string explain = ;
amount = textBox1.Text.Trim();
people = textBox2.Text.Trim();
place = textBox3.Text.Trim();
explain = textBox4.Text.Trim();
先定义变量再赋值就好了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
向空的SQL数据表 意思是说,,要向数据库些数据吗?要是这样的话,把SQL语句改一下,就行了.
INSERT INTO [表名] (字段1,字段2,字段3) VALUES(值1,值2,值3)
意思要是在页面上显示数据的话? 就直接添加一个 GrideView控件,将它的DataSourse属性等于ds1就行了
PS: 你的前六行代码等同于 string kind = radioButton1.Checked?"收入":"支出";
INSERT INTO [表名] (字段1,字段2,字段3) VALUES(值1,值2,值3)
意思要是在页面上显示数据的话? 就直接添加一个 GrideView控件,将它的DataSourse属性等于ds1就行了
PS: 你的前六行代码等同于 string kind = radioButton1.Checked?"收入":"支出";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询