怎么用循环把数据插入到数据库表中(.net)
4个回答
展开全部
for (int i = 0; i < dt.Rows.Count; i++)
{
string strex = "insert into pub_ExcellenceProject(id,projectCode,mainid,bank,Fujia,newTechnologyProject,GreenBuild,Others,insertUserName,insertTime,updateUserName,updateTime)";
strex += " VALUES ( " + SysConvert.ToDBString(dt.Rows[i]["id"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["projectCode"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["mainid"].ToString());
strex += " , " + SysConvert.ToInt32(dt.Rows[i]["bank"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["Fujia"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["newTechnologyProject"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["GreenBuild"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["Others"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["insertUserName"].ToString());
strex += " ," + SysConvert.ToDBString(SysConvert.ToDateTimeString(DateTime.Parse(dt.Rows[i]["insertTime"].ToString())));
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["updateUserName"].ToString());
strex += " ," + SysConvert.ToDBString(SysConvert.ToDateTimeString(DateTime.Parse( dt.Rows[i]["updateTime"].ToString())));
strex += " )";
sqlcmd.CommandText = strex;
sqlcmd.ExecuteNonQuery();
}
就可以了
dt是一个数据源
filePath文件路径
filePostfix 文件后缀
public bool InsertAppurtenance(string filePath, string fileName, string filePostfix,string remark,int isshare)
{
bool flag = false;
SqlCommand comm = (SqlCommand)SysUtil.GetCommand();
//判断文件名称的长度
if (fileName.Length > 300)
{
fileName = fileName.Substring(0, 300);
}
try
{
//内容
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] fileContent = new byte[fs.Length];
fs.Read(fileContent, 0, (int)fs.Length);
fs.Close();
//文件大小
decimal size = Convert.ToDecimal(FileSize(filePath));
//组合sql语句
string strSql = string.Empty;
strSql = "INSERT INTO jzs_FileInfoDir(ID,fatherProjectCode,fileName,ParentID,fileContent,filePostfix,IsDirectory,IsShare,userId,remark,insertTime,insertUserName,updateTime,updateUserName) VALUES(";
strSql += "'" + Guid.NewGuid().ToString();///
strSql += "','" + ProjectInfo.FatherProjectCode + "',";
strSql += "'" + fileName + "',";
strSql += "'" + pid + "',";
strSql += "@FileContent,";
strSql += "'" + filePostfix + "',";
strSql += "0 ";
strSql += " ," + SysConvert.ToInt32(isshare);
strSql += "," + SysConvert.ToDBString(manager);
strSql += " ,"+SysConvert.ToDBString(remark);
strSql += ", " + SysConvert.ToDBString(SysConvert.ToDateTimeString(SystemInfo.CurrentDate))+ ",";
strSql += "'" + LoginUserInfo.Id + "'";
strSql += "," +SysConvert.ToDBString(SysConvert.ToDateTimeString(SystemInfo.CurrentDate))+ ",";
strSql += "'" + LoginUserInfo.Id + "'";
strSql += ")";
comm.CommandText = strSql;
SqlParameter para = new SqlParameter("@FileContent", SqlDbType.Image);
para.Value = fileContent;
comm.Parameters.Add(para);
comm.ExecuteNonQuery();
flag= true;
}
catch (Exception)
{
MessageBox.Show(fileName + "." + filePostfix + "已经被打开,请关闭后重新添加。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
flag= false;
}
finally
{
comm.Connection.Close();
}
return flag;
}
{
string strex = "insert into pub_ExcellenceProject(id,projectCode,mainid,bank,Fujia,newTechnologyProject,GreenBuild,Others,insertUserName,insertTime,updateUserName,updateTime)";
strex += " VALUES ( " + SysConvert.ToDBString(dt.Rows[i]["id"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["projectCode"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["mainid"].ToString());
strex += " , " + SysConvert.ToInt32(dt.Rows[i]["bank"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["Fujia"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["newTechnologyProject"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["GreenBuild"].ToString());
strex += " ," + SysConvert.ToDBString(dt.Rows[i]["Others"].ToString());
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["insertUserName"].ToString());
strex += " ," + SysConvert.ToDBString(SysConvert.ToDateTimeString(DateTime.Parse(dt.Rows[i]["insertTime"].ToString())));
strex += " , " + SysConvert.ToDBString(dt.Rows[i]["updateUserName"].ToString());
strex += " ," + SysConvert.ToDBString(SysConvert.ToDateTimeString(DateTime.Parse( dt.Rows[i]["updateTime"].ToString())));
strex += " )";
sqlcmd.CommandText = strex;
sqlcmd.ExecuteNonQuery();
}
就可以了
dt是一个数据源
filePath文件路径
filePostfix 文件后缀
public bool InsertAppurtenance(string filePath, string fileName, string filePostfix,string remark,int isshare)
{
bool flag = false;
SqlCommand comm = (SqlCommand)SysUtil.GetCommand();
//判断文件名称的长度
if (fileName.Length > 300)
{
fileName = fileName.Substring(0, 300);
}
try
{
//内容
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] fileContent = new byte[fs.Length];
fs.Read(fileContent, 0, (int)fs.Length);
fs.Close();
//文件大小
decimal size = Convert.ToDecimal(FileSize(filePath));
//组合sql语句
string strSql = string.Empty;
strSql = "INSERT INTO jzs_FileInfoDir(ID,fatherProjectCode,fileName,ParentID,fileContent,filePostfix,IsDirectory,IsShare,userId,remark,insertTime,insertUserName,updateTime,updateUserName) VALUES(";
strSql += "'" + Guid.NewGuid().ToString();///
strSql += "','" + ProjectInfo.FatherProjectCode + "',";
strSql += "'" + fileName + "',";
strSql += "'" + pid + "',";
strSql += "@FileContent,";
strSql += "'" + filePostfix + "',";
strSql += "0 ";
strSql += " ," + SysConvert.ToInt32(isshare);
strSql += "," + SysConvert.ToDBString(manager);
strSql += " ,"+SysConvert.ToDBString(remark);
strSql += ", " + SysConvert.ToDBString(SysConvert.ToDateTimeString(SystemInfo.CurrentDate))+ ",";
strSql += "'" + LoginUserInfo.Id + "'";
strSql += "," +SysConvert.ToDBString(SysConvert.ToDateTimeString(SystemInfo.CurrentDate))+ ",";
strSql += "'" + LoginUserInfo.Id + "'";
strSql += ")";
comm.CommandText = strSql;
SqlParameter para = new SqlParameter("@FileContent", SqlDbType.Image);
para.Value = fileContent;
comm.Parameters.Add(para);
comm.ExecuteNonQuery();
flag= true;
}
catch (Exception)
{
MessageBox.Show(fileName + "." + filePostfix + "已经被打开,请关闭后重新添加。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
flag= false;
}
finally
{
comm.Connection.Close();
}
return flag;
}
展开全部
先FOR(要插入的数据总行总)
{
插入操作
}
{
插入操作
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是要把一张表里符合要求的数据查到另一张表里吗?
可以用T-SQL实现的
可以用T-SQL实现的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询