终于解决了,网上很多教程 害人不浅啊,用了一大堆垃圾代码。
我的程序:
SqlDataSource2.SelectCommand = "Select count(*) from [TBFiscalCalendar] where YearMonth = '" + NewYM + "'";
SqlConnection myCon=new SqlConnection(SqlDataSource2.ConnectionString);
SqlCommand myCom = new SqlCommand(SqlDataSource2.SelectCommand,myCon);
myCon.Open();
int countD=(int)myCom.ExecuteScalar();
myCon.Close();
if (countD == 0)
{
SqlDataSource2.InsertCommand = "INSERT INTO [TBFiscalCalendar] ([YearMonth]) VALUES ("+NewYM+")";
SqlDataSource2.Insert();
}
SqlDataSource2是非棒定数据源。只是一个为了判断和插入记录的