![](/img/trans.png)
[英]I need to create a sql server database table using visual studio(c# language) . But the name of the table has to be an input from the user
[英]I need to get the Dataset from SQL from Table Valued Function using code C# in visual studio
我有一个 SQL 表。
然后我创建了一个名为 GetTaxDataSet 的表值函数。 因此,当我使用 Where 条件执行该函数时,我将获得所需的数据集值。
现在,我正在尝试使用 C# 代码使用 SQL 表值函数获取数据集。
DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(); string sqlSelect = "Select * From [dbo].[fn_GetTaxRuleDataSet] (@TaxID, @TaxRulesID)"; SqlCommand cmd = new SqlCommand(sqlSelect, Sqlconn); SqlParameter param1 = new SqlParameter(); param1.ParameterName = "@ID"; param1.SqlDbType = SqlDbType.Int; param1.ParameterName = "@TaxID"; param1.SqlDbType = SqlDbType.Int; param1.ParameterName = "TaxRulesID"; param1.SqlDbType = SqlDbType.Int; param1.ParameterName = "@EffectiveDate"; param1.SqlDbType = SqlDbType.DateTime; cmd.Parameters.Add(param1); Sqlconn.Open(); //Executing The SqlCommand. SqlDataReader dr = cmd.ExecuteReader(); Console.ReadLine();
你们能建议我用正确的代码吗? 谢谢
希望这个回答对你有帮助。 请记住,当您创建一个返回类型为 DataSet 的方法时,它也应该设置为接收 DataSet。
public DataSet GetData()
{
DataSet output = new DataSet();
try
{
SqlConnection sqlConn = new SqlConnection(connectionString);
if (sqlConn.State == ConnectionState.Closed)
{
sqlConn.Open();
}
String sqlSelect = "Select * From [dbo].[fn_GetTaxRuleDataSet] (@TaxID, @TaxRulesID)";
SqlCommand cmd = new SqlCommand(sqlSelect, sqlConn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(output);
sqlConn.Close();
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", ex.Message, true);
return (null);
}
return output;
}
下面的代码有效。 这是用于表值函数。 试图从 c# 检索数据。
public void fn_GetTaxCalculationDataSet(int cur_tax_calc_ruleid) { Sqlconn.Open(); //连接打开
//Variables to Store Result from DataSet
int TaxCalcRuleID = cur_tax_calc_ruleid;
int ScheduleTypeID = 0;
int FilingStatusID = 0;
int bottomorder = 0;
decimal bottom = 0;
decimal Amount = 0;
decimal plusAmountOfPercentage = 0;
try
{
//Defining The Query.
string sqlSelect = "Select * From dbo.[fn_GetTaxCalculationDataSet] (@TaxCalcRuleID)";
SqlCommand cmd = new SqlCommand(sqlSelect, Sqlconn);
cmd.Parameters.Add("@TaxCalcRuleID", SqlDbType.Int).Value = cur_tax_calc_ruleid;
//Executing The SqlCommand
SqlDataReader dr = cmd.ExecuteReader();
Console.WriteLine(Environment.NewLine + "Retrieving CalcData based on TaxRuleID From database..!" + Environment.NewLine);
Console.WriteLine("Retriving The Records");
Console.WriteLine("Press Any Key To Continue.....!");
Console.ReadLine();
//Check if have any records..!
if (dr.HasRows)
{
while (dr.Read())
{
TaxCalcRuleID = dr.GetInt32(0);
ScheduleTypeID = dr.GetInt32(1);
FilingStatusID = dr.GetInt32(2);
bottomorder = dr.GetInt32(3);
bottom = dr.GetDecimal(4);
Amount = dr.GetDecimal(5);
plusAmountOfPercentage = dr.GetDecimal(6);
//Displaying The Retrieved Records or DataSet..!
Console.WriteLine("{0},{1},{2},{3},{4},{5},{6}", TaxCalcRuleID.ToString(), ScheduleTypeID, FilingStatusID, bottomorder, bottom, Amount, plusAmountOfPercentage);
Console.WriteLine("Press Enter To Close");
Console.ReadLine();
}
}
else
{
Console.WriteLine("No Data Found");
}
//Data ReaderClosed.
dr.Close();
//SQL Connection Closed.
Sqlconn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.