C#连接SQL数据库
代码如下:string sql1 = string.Format("select datediff(MONTH,(select DateOfProduction from Assets where DateOfProduction='{0}'),GETDATE())", this.date1.Text);
object bt1 = DBHelper.ExecuteScalar(sql1);
int d;
//txtBox11为输入的预计净残值
double a=double.Parse(this.txtBox11.Text);
//txtBox13为资产原值
double b =double.Parse( this.txtBox13.Text);
//折旧月数
x = double.Parse(bt1.ToString());左边数据库显示
右边c#运行显示
代码如下:string sql1 = string.Format("select datediff(MONTH,(select DateOfProduction from Assets where DateOfProduction='{0}'),GETDATE())", this.date1.Text);
object bt1 = DBHelper.ExecuteScalar(sql1);
int d;
//txtBox11为输入的预计净残值
double a=double.Parse(this.txtBox11.Text);
//txtBox13为资产原值
double b =double.Parse( this.txtBox13.Text);
//折旧月数
x = double.Parse(bt1.ToString());左边数据库显示
右边c#运行显示
MONTH,
(
SELECT DateOfProduction
FROM Assets
WHERE DateOfProduction = '{0}'
),
GETDATE()
)
datediff 是一个标量函数, 只能计算两个标量值的差。
GETDATE() 是标量无疑问。
但
SELECT DateOfProduction FROM Assets WHERE DateOfProduction = '{0}'
这个可能输出一个值,更有可能是多个值, 无法确定。
如果输出多个值, 那么 SQL Server 无法比较, 则会报你上面的错误。知道了原因, 再来改就好说了。
如果你想以最大值来比较,则改为 SELECT max(DateOfProduction) FROM Assets WHERE DateOfProduction = '{0}'
反正最后结果只能有一条记录。