我不能将我的数据存储在C#Sql Server的数据库中 [英] I Cant I Store My Data In Database In C# Sql Server
问题描述
otected void Button1_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(Data Source = .; Initial Catalog = demo1; Integrated Security =真);
con.Open();
隐藏展开复制代码
int a =转换.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
if(DropDownList1.Text = =add)
{
int sum =(a + b);
Label1.Text = Convert.ToString(sum);
dat.Text = DateTime.Now.ToString();
}
如果
(DropDownList1.Text ==sub)
{
int sub =(a - b);
Label1.Text = Convert.ToString(sub);
dat.Text = DateTime.Now.ToString();
}
如果
(DropDo wnList1.Text ==mul)
{
int mul = a * b;
Label1.Text = Convert.ToString(mul );
dat.Text = DateTime.Now.ToString();
}
如果
(DropDownList1) .Text ==div)
{
十进制div = a / b;
Label1.Text = Convert.ToString(div) ;
dat.Text = DateTime.Now.ToString();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =p;
cmd.Parameters.AddWithValue(@ firsttno,TextBox1.Text);
cmd.Parameters.AddWithValue(@ secondno,TextBox2.Text);
cmd.Parameters.AddWithValue(@ operation,DropDownList1.Text);
cmd.Parameters.AddW ithValue(@ result,Label1.Text);
cmd.Parameters.AddWithValue(@ optime,dat.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
}
}
otected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=demo1;Integrated Security=True");
con.Open();
Hide Expand Copy Code
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
if (DropDownList1.Text == "add")
{
int sum = (a + b);
Label1.Text = Convert.ToString(sum);
dat.Text = DateTime.Now.ToString();
}
if
(DropDownList1.Text == "sub")
{
int sub = (a - b);
Label1.Text = Convert.ToString(sub);
dat.Text = DateTime.Now.ToString();
}
if
(DropDownList1.Text == "mul")
{
int mul = a * b;
Label1.Text = Convert.ToString(mul);
dat.Text = DateTime.Now.ToString();
}
if
(DropDownList1.Text == "div")
{
decimal div = a / b;
Label1.Text = Convert.ToString(div);
dat.Text = DateTime.Now.ToString();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "p";
cmd.Parameters.AddWithValue("@firsttno", TextBox1.Text);
cmd.Parameters.AddWithValue("@secondno", TextBox2.Text);
cmd.Parameters.AddWithValue("@operation", DropDownList1.Text);
cmd.Parameters.AddWithValue("@result", Label1.Text);
cmd.Parameters.AddWithValue("@optime", dat.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
}
}
}
推荐答案
这是因为你没有得到任何保存价值。
你给的if条件错了..
应该是
if(DropDownList1.SelectedValue ==add)
{
}
Bro this is because you are not getting any value to save.
The if Condition you gave is wrong..
It should be
if (DropDownList1.SelectedValue== "add")
{
}
创建程序p
(
@firsttno int ,
@secondno int,
@operation varchar(max),
@result int,
@optime datetime
)
as begin
从table1中选择firstno,secondno,operation,result,optime,其中firstno = @ firsttno和secondno = @ secondno和operation = @ operation和result = @ result和optime = @ optime
end
create procedure p
(
@firsttno int,
@secondno int,
@operation varchar(max),
@result int,
@optime datetime
)
as begin
select firstno,secondno,operation,result,optime from table1 where firstno=@firsttno and secondno=@secondno and operation=@operation and result=@result and optime=@optime
end
创建程序p
(
@firsttno int,
@secondno int,>
@operation varchar(max),
@result int,
@optime datetime
)
as begin
插入table1(firstno,secondno,operation,result,optime)值(@ firsttno,@ secondno,@ operation,@ result,@ optime)
结束
在C#代码中
create procedure p
(
@firsttno int,
@secondno int,
@operation varchar(max),
@result int,
@optime datetime
)
as begin
Insert into table1(firstno,secondno,operation,result,optime) Values(@firsttno,@secondno,@operation,@result,@optime)
end
In C# code
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "p";
cmd.Parameters.AddWithValue("@firsttno", TextBox1.Text);
cmd.Parameters.AddWithValue("@secondno", TextBox2.Text);
cmd.Parameters.AddWithValue("@operation", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@result", Label1.Text);
cmd.Parameters.AddWithValue("@optime", dat.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
这篇关于我不能将我的数据存储在C#Sql Server的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!