就是從兩個textbox獲取兩個日期,再在表格中列出這兩個日期中的值,下面的哪裏出錯了? protected void Check_Click(object sender, EventArgs e)
{
try
{
DateTime from = DateTime.Parse(txtfrom.Text);
DateTime to = DateTime.Parse(txtto.Text);//獲取textbox的值
warn.Visible = false;
SqlConnection conn = new SqlConnection(
@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Mydb");
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
cmd.Connection = conn;
conn.Open(); cmd.CommandText = "OrdersbyDate";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Beginning_Date", SqlDbType.DateTime).Value = from;
cmd.Parameters.Add("@Ending_Date", SqlDbType.DateTime).Value = to;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception a)
{
warn.Text = "Invalid Date e.g. 200X-X-X";
warn.Visible = true;
}
}
{
try
{
DateTime from = DateTime.Parse(txtfrom.Text);
DateTime to = DateTime.Parse(txtto.Text);//獲取textbox的值
warn.Visible = false;
SqlConnection conn = new SqlConnection(
@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Mydb");
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
cmd.Connection = conn;
conn.Open(); cmd.CommandText = "OrdersbyDate";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Beginning_Date", SqlDbType.DateTime).Value = from;
cmd.Parameters.Add("@Ending_Date", SqlDbType.DateTime).Value = to;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception a)
{
warn.Text = "Invalid Date e.g. 200X-X-X";
warn.Visible = true;
}
}
解决方案 »
- C#如何定义一个不固定长度的数组?数组中的内容来自表中的内容,如何做?
- 关于Entity Framewrok4 性能问题的疑问
- 请问Static Nested Class 和 Inner Class 的区别?
- 急!向数据库中插入数据出错
- 如何获取模态窗口的返回值?
- 关于VSS的问题想请教大家???
- 如何在通过网页实现把两个客户点对点连接起来
- asp.net如何读取windows media service直播广播中的节目清单及当前正在播放的节目?
- 关于 DataGrid 和 输入验证的问题!!急!希望高手解答!
- 求solrnet的开发实例,最好是在项目中用过的,有谁可以帮帮忙吗
- 怎么把扩展名为.js页面调用到.net页面中
- 大家快来看看 这个时间怎么处理的
DataReader在使用过程中不需要手动打开连接源其次,楼主使用将sql语句赋给command对象,但是却使用DataAdapter.Fill方法填充数据,可见楼主应该是初学者DataAdapter要想填充数据,必须将sql语句赋给DataAdapter才行,否则DataAdapter不包含任何可执行命令,怎么可能能够获得数据?
try
{
DateTime from = DateTime.Parse(txtfrom.Text);
DateTime to = DateTime.Parse(txtto.Text);//獲取textbox的值
warn.Visible = false;
SqlConnection conn = new SqlConnection(
@"Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=Mydb");
conn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(cmd); //这里传进command对象,忘了这个是不是一个参数了
cmd.Connection = conn;
//conn.Open(); 不需要手动打开数据源 cmd.CommandText = "OrdersbyDate";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Beginning_Date", SqlDbType.DateTime).Value = from;
cmd.Parameters.Add("@Ending_Date", SqlDbType.DateTime).Value = to;
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception a)
{
warn.Text = "Invalid Date e.g. 200X-X-X";
warn.Visible = true;
}
但是,我按照您的做了修改,輸入兩個參數之後,卻出現了“无法显示网页,错误代码:500 ”
會不會是因爲我是xphome,沒有iis的原因嘛?
但是,我按照您的做了修改,輸入兩個參數之後,卻出現了“无法显示网页,错误代码:500 ”
會不會是因爲我是xphome,沒有iis的原因嘛?
string queryString, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(queryString, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter); connection.Open(); DataSet dataSet = new DataSet();
adapter.Fill(dataSet, tableName); //code to modify data in DataSet here builder.GetUpdateCommand(); //Without the SqlCommandBuilder this line would fail
adapter.Update(dataSet, tableName); return dataSet;
}
}SqlDataAdapter 不会自动生成实现 DataSet 的更改与关联的 SQL Server 实例之间的协调所需的 Transact-SQL 语句。但是,如果设置了 SqlDataAdapter 的 SelectCommand 属性,则可以创建一个 SqlCommandBuilder 对象来自动生成用于单表更新的 Transact-SQL 语句。然后,SqlCommandBuilder 将生成其他任何未设置的 Transact-SQL 语句。