1个回答
展开全部
给你段代码:这是我现写的 着忙 代码可以简略:
public string FirstTab()
{
DataTable dt = new DataTable();
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();
string strsql = "select name from userInfo";
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlConn);
sda.Fill(dt);
string names = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
names = dt.Rows[i][0].ToString() + ",";
}
sqlConn.Close();
return names;
}
public void Insert()
{
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();
string name = FirstTab();
string[] names = name.Split(',');
for (int i = 0; i < names.Length; i++)
{
string strsql = "Insert 表(字段)values('"+ names[i] +"')";
SqlCommand com = new SqlCommand(strsql, sqlConn);
com.ExecuteNonQuery();
}
}
public string FirstTab()
{
DataTable dt = new DataTable();
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();
string strsql = "select name from userInfo";
SqlDataAdapter sda = new SqlDataAdapter(strsql, sqlConn);
sda.Fill(dt);
string names = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
names = dt.Rows[i][0].ToString() + ",";
}
sqlConn.Close();
return names;
}
public void Insert()
{
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();
string name = FirstTab();
string[] names = name.Split(',');
for (int i = 0; i < names.Length; i++)
{
string strsql = "Insert 表(字段)values('"+ names[i] +"')";
SqlCommand com = new SqlCommand(strsql, sqlConn);
com.ExecuteNonQuery();
}
}
追问
用UPDATE写 怎么写
public bool Update(string namea, string qpwd, string newpwd)
{
DataMenthod dm = new DataMenthod();
string sqlret = "update byuser set PassWord='userpwd'";
//string sqlret = "update datapwd set userpwd='PassWord'";
dm.DataMenthodAdd(sqlret,ui);
return true
}
我是这么写的
追答
这个也是一样的啊 你不是要更新一列字段吗?
那就
public void update(string na)
{
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
if (sqlConn.State != ConnectionState.Open)
sqlConn.Open();
string name = FirstTab();
string[] names = name.Split(',');
for (int i = 0; i < names.Length; i++)
{
na=names[i].tostring
string strsql = "update datapwd set userpwd='"+ na +"'" ";
SqlCommand com = new SqlCommand(strsql, sqlConn);
com.ExecuteNonQuery();
}
}
这就好了 你要确保查出来的字段数要与 修改表的字段数相同。 不同的话你需要加一个判断
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询