这是情景:
我需要在我的数据库中插入一些行,其中一列是DateTime类型,其中SysDateTime为Value,通常我的查询看起来像这样
String sqlString = "INSERT INTO Temas(Id, Level,Date, Description) " +
" VALUES (100, 3, SYSDATETIME() , 'Text ')";
现在我必须使用参数进行插入,现在它看起来像这样:
private void InsertTemaInTesauro(OleDbDataReader origenReader,Materias materia)
{
SqlConnection connectionEpsOle = Conexiones.GetConecction();
SqlDataAdapter dataAdapter;
DataSet dataSet = new DataSet();
DataRow dr;
string sqlCadena = "SELECT * FROM Temas WHERE idTema = 0";
dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = new SqlCommand(sqlCadena, connectionEpsOle);
dataAdapter.Fill(dataSet, "Temas");
dr = dataSet.Tables["Temas"].NewRow();
dr["Id"] = 100;
dr["Level"] = 3;
dr["Date"] = ????;
dr["Description"] = 'Some text';
dataSet.Tables["Temas"].Rows.Add(dr);
//dataAdapter.UpdateCommand = connectionEpsOle.CreateCommand();
dataAdapter.InsertCommand = connectionEpsOle.CreateCommand();
dataAdapter.InsertCommand.CommandText =
"INSERT INTO Temas(Id,Level,Date,Description) (@Id,@Level,@Date,@Description)";
dataAdapter.InsertCommand.Parameters.Add("@Id", SqlDbType.Numeric, 0, "Id");
dataAdapter.InsertCommand.Parameters.Add("@Level", SqlDbType.Numeric, 0, "Level");
dataAdapter.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime, 0, "Date");
dataAdapter.InsertCommand.Parameters.Add("@Description", SqlDbType.Numeric, 0, "Description");
dataAdapter.Update(dataSet, "Temas");
dataSet.Dispose();
dataAdapter.Dispose();
connectionEpsOle.Close();
}
我的问题是如何设置日期参数取sysdatetime()的值,我不能将其设置为列的默认值,因为我无法访问数据库,我无法使用DateTime现在因为老板想要数据库的时间
答案 0 :(得分:0)
我很确定你可以这样做:
dataAdapter.InsertCommand.CommandText =
"INSERT INTO Temas(Id,Level,Date,Description) (@Id,@Level,SYSDATETIME(),@Description)"
答案 1 :(得分:0)
你能做到吗
dataAdapter.InsertCommand.CommandText =“INSERT INTO Temas(Id,Level,Date,Description)(@ Id,@ Level,SYSDATETIME(),@ Description)”;
答案 2 :(得分:0)
你能试试吗?我现在正打电话,所以这可能不是确切的答案,但你会得到它 -
dataAdapter.InsertCommand.CommandText = "INSERT INTO Temas(Id,Level,Date, Description) (@Id,@Level, SYSDATETIME(), @Description")";