我的任务是创建一个控制台应用程序,该应用程序显示作业输入系统的空值(员工用来声明他们正在处理哪个客户端的程序等)。
我所有被告知的是,存在一个名为spr_Reports_JobsBreakdown4
的存储过程,其中包含以下参数:
@user nvarchar(20)
@datefrom datetime
@dateto datetime
我不得不创建一个控制台应用程序,它使用这个存储过程来显示空值,这意味着有人忘了做输入。到目前为止,我所管理的只是连接数据库..
我已经观看并阅读了教程,但似乎没有任何帮助。
我是C#的新手..帮忙?!?
这是我用来连接数据库的代码。
SqlConnection sqlConnection = new SqlConnection("Data Source=DEFIANT\\SQL2012; Initial Catalog=HRD_MIS_Jobs2009; User ID=id; Password=password");
try
{
sqlConnection.Open();
Console.WriteLine("successful connection");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
答案 0 :(得分:1)
你基本上需要这样的东西:
这样的代码:
string connectionString = "Data Source=DEFIANT\\SQL2012; Initial Catalog=HRD_MIS_Jobs2009; User ID=id; Password=password";
string storedProcedureName = "dbo.spr_Reports_JobsBreakdown4";
// establish connection to DB, define command to execute stored procedure
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(storedProcedureName, conn))
{
try
{
// set type of command to stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// define parameters
cmd.Parameters.Add("@user", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@datefrom", SqlDbType.DateTime);
cmd.Parameters.Add("@dateto", SqlDbType.DateTime);
conn.Open();
Console.WriteLine("successful connection");
// set parameter values
cmd.Parameters["@user"].Value = ......;
cmd.Parameters["@datefrom"].Value = ......;
cmd.Parameters["@dateto"].Value = ......;
// execute stored procedure, handle return values
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// handle your data here.....
}
reader.Close();
}
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
答案 1 :(得分:0)
好的,下一步,在连接到数据库后,您需要一种方法来发出命令。你可以这样做:
//Create a command object
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Text = "spr_Reports_JobsBreakdown4";
cmd.Connection = sqlConnection;
//Create its parameters
SqlParameter param1 = new SqlParameter("@user", SqlDbType.NVarChar);
param1.value = "someusername";
SqlParameter param2 = new SqlParameter("@datefrom", SqlDbType.DateTime);
param2.value = DateTime.Now - 1; //this should result in yesterday date/time
SqlParameter param3 = new SqlParameter("@datefto", SqlDbType.DateTime);
param3.value = DateTime.Now;
//Add parameters to the command
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);
//Execute your command this way if the procedure returns just a single value
var res = cmd.ExecuteScalar();
//... Or else use a reader
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// you can read specific items from your reader with
var col1Content = reader("col1");
}
reader.Close();
}
我希望这足以让你指出进一步研究的正确方向。
答案 2 :(得分:0)
在app.config中
<add name="DataSource" providerName="System.Data.SqlClient" connectionString="[connection string details]"/>
</connectionStrings>
然后在数据访问类
string user;
datetime dateFrom;
datetime DateTo;
//populate parameters...
ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["DataSource"];
try
{
using (SqlConnection conn = new SqlConnection(connectionString.ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("spr_Reports_JobsBreakdown4 ", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@user", user));
cmd.Parameters.Add(new SqlParameter("@datefrom", dateFrom));
cmd.Parameters.Add(new SqlParameter("@dateto", dateTo));
reader = cmd.ExecuteReader();
while (reader.Read())
{
//use reader to read in each returned row
}
reader.close();
}
}
}
catch(Exception ex){
//handled exception
}