问题描述
我要做一个可以对表新增,修改,删除的程序,初学者..请赐教。
解决方案
解决方案二:
用SqlCommand类的方法
解决方案三:
//连接数据库stringstrConnection="Provider=Microsoft.Jet.OleDb.4.0;";strConnection+=@"DataSource=DataBaseDB.mdb";OleDbConnectionConn=newOleDbConnection(strConnection);//stringdw=this.txtdwmc.Text.ToString();//stringjsyxm=this.txtjsyxm.Text.ToString();//添加记录时的SQL命令stringinsert="INSERTINTOjiaotong(dw,jsyxm,jszh,lxdh,jsyqz)VALUES('"+this.txtdwmc.Text+"','"+this.txtjsyxm.Text+"','"+this.txtjszh.Text+"','"+this.txtlxdh.Text+"','"+this.txtbz.Text+"')";//初始化OleDbCommandOleDbCommandComm=newOleDbCommand();Comm.CommandText=insert;Comm.Connection=Conn;//打开连接Conn.Open();//执行添加记录的语句Comm.ExecuteNonQuery();//关闭连接Conn.Close();
解决方案四:
同意楼上up
解决方案五:
//与SQL的连接//查询SqlConnectionconn=newSqlConnection("server=localhost;database=northwind;uid=sa;pwd=;");try{SqlCommandcmd=conn.CreateCommand();cmd.CommandText="select*fromcategories";cmd.CommandType=CommandType.Text;conn.Open();SqlDataReaderdatareader=cmd.ExecuteReader();//以下是把查询出来的数据直接生成表Response.Write("<tableborder=1");Response.Write("<tr><th>CategoryID</th><th>CategoryName</th><th>Description</th></tr>");while(datareader.Read()){Response.Write("<tr>");Response.Write("<tdalign='center'>"+datareader["CateGoryID"]+"</td>");Response.Write("<tdalign='center'>"+datareader["Categoryname"]+"</td>");Response.Write("<tdalign='left'>"+datareader["Description"]+"</td>");Response.Write("</tr>");}Response.Write("</table>");datareader.Close();conn.Close();}finally{if(conn!=null)conn.Dispose();}//增加记录try{SqlCommandcmd=conn.CreateCommand();cmd.CommandText="insertinto表名(列1,列2,列3)values(@列1,@列2,@列2)categories";cmd.CommandType=CommandType.Text;conn.Open();SqlDataReaderdatareader=cmd.ExecuteReader();Response.Write("<tableborder=1");Response.Write("<tr><th>CategoryID</th><th>CategoryName</th><th>Description</th></tr>");while(datareader.Read()){Response.Write("<tr>");Response.Write("<tdalign='center'>"+datareader["CateGoryID"]+"</td>");Response.Write("<tdalign='center'>"+datareader["Categoryname"]+"</td>");Response.Write("<tdalign='left'>"+datareader["Description"]+"</td>");Response.Write("</tr>");}Response.Write("</table>");datareader.Close();conn.Close();}finally{if(conn!=null)conn.Dispose();}
解决方案六:
//与SQL的连接//查询SqlConnectionconn=newSqlConnection("server=localhost;database=northwind;uid=sa;pwd=;");try{SqlCommandcmd=conn.CreateCommand();cmd.CommandText="select*fromcategories";cmd.CommandType=CommandType.Text;conn.Open();SqlDataReaderdatareader=cmd.ExecuteReader();//以下是把查询出来的数据直接生成表Response.Write("<tableborder=1");Response.Write("<tr><th>CategoryID</th><th>CategoryName</th><th>Description</th></tr>");while(datareader.Read()){Response.Write("<tr>");Response.Write("<tdalign='center'>"+datareader["CateGoryID"]+"</td>");Response.Write("<tdalign='center'>"+datareader["Categoryname"]+"</td>");Response.Write("<tdalign='left'>"+datareader["Description"]+"</td>");Response.Write("</tr>");}Response.Write("</table>");datareader.Close();conn.Close();}finally{if(conn!=null)conn.Dispose();}//增加记录try{SqlCommandcmd=conn.CreateCommand();cmd.CommandText="insertinto表名(列1,列2,列3...)values(@列1,@列2,@列2...)categories";cmd.CommandType=CommandType.Text;//设置参数列1SqlParameter列1=newSqlPrameter();列1.ParameterName="@列1";列1.DbType=DbType.String;cmd.Parameters.Add(列1);//设置参数列2SqlParameter列2=newSqlPrameter();列2.ParameterName="@列2";列2.DbType=DbType.String;cmd.Parameters.Add(列2);//设置参数列3SqlParameter列3=newSqlPrameter();列3.ParameterName="@列3";列3.DbType=DbType.String;cmd.Parameters.Add(列3);//......//执行SQL语名并且返回影响行数conn.Open();introwAffected=0rowAffected=cmd.ExecuteNonQuery();conn.Close();}finally{if(conn!=null)conn.Dispose();}
解决方案七:
最简单的是否可以一个控件就能实现呢?
解决方案八:
欢迎大家加入群:10987609讨论各项技术问题
解决方案:
用sqlDataSource或AccessDataSource控件,然後根據向導去設置就可以了
解决方案:
up
解决方案:
有很多方法的,我用的是对存储过程的操作.封装一下,哪里都可以用,只要把存储过程的名称写入就能得到DataSet或者SqlDataReader.网上有很多.如果找不到,我可以发一个给你.
解决方案:
谢谢各位!
解决方案:
privatevoidForm1_Load(objectsender,System.EventArgse){con=newSqlConnection("database=master;uid=sa");ds=newDataSet();da=newSqlDataAdapter("select*fromaxl1",con);da.Fill(ds,"axl1");dataGrid1.DataSource=ds.Tables[0].DefaultView;if(ds.Tables["axl1"].Rows.Count==0){bntdelete.Enabled=false;btnupdate.Enabled=false;}}privatevoidbtninsert_Click(objectsender,System.EventArgse){con.Open();stringstr="insertintoaxl1values(@FlightCode,@Password,@Name,@SeatNo,@Sex,@Age)";da.InsertCommand=newSqlCommand(str,con);sp=da.InsertCommand.Parameters.Add("@FlightCode",SqlDbType.Char);sp.SourceColumn="FlightCode";sp.SourceVersion=DataRowVersion.Current;sp=da.InsertCommand.Parameters.Add("@Password",SqlDbType.VarChar);sp.SourceColumn="Password";sp.SourceVersion=DataRowVersion.Current;sp=da.InsertCommand.Parameters.Add("@Name",SqlDbType.VarChar);sp.SourceColumn="Name";sp.SourceVersion=DataRowVersion.Current;sp=da.InsertCommand.Parameters.Add("@SeatNo",SqlDbType.Int);sp.SourceColumn="SeatNo";sp.SourceVersion=DataRowVersion.Current;sp=da.InsertCommand.Parameters.Add("@Sex",SqlDbType.VarChar);sp.SourceColumn="Sex";sp.SourceVersion=DataRowVersion.Current;sp=da.InsertCommand.Parameters.Add("@Age",SqlDbType.VarChar);sp.SourceColumn="Age";sp.SourceVersion=DataRowVersion.Current;try{da.Update(ds,"axl1");MessageBox.Show("已经插入");}catch(Exceptionex){MessageBox.Show(ex.ToString());MessageBox.Show("插入失败");}finally{con.Close();}if(ds.Tables["axl1"].Rows.Count>1){bntdelete.Enabled=true;btnupdate.Enabled=true;}}privatevoidbtnupdate_Click(objectsender,System.EventArgse){stringupdate="updateaxl1setFlightCode=@FlightCode,Password=@Password,Name=@Name,SeatNo=@SeatNo,Sex=@Sex,Age=@AgewhereFlightCode=@FlightCode";da.UpdateCommand=newSqlCommand(update,con);sp=da.UpdateCommand.Parameters.Add("@FlightCode",SqlDbType.VarChar);sp.SourceColumn="FlightCode";sp.SourceVersion=DataRowVersion.Current;sp=da.UpdateCommand.Parameters.Add("@Password",SqlDbType.VarChar);sp.SourceColumn="Password";sp.SourceVersion=DataRowVersion.Current;sp=da.UpdateCommand.Parameters.Add("@Name",SqlDbType.VarChar);sp.SourceColumn="Name";sp.SourceVersion=DataRowVersion.Current;sp=da.UpdateCommand.Parameters.Add("@SeatNo",SqlDbType.Int);sp.SourceColumn="SeatNo";sp.SourceVersion=DataRowVersion.Current;sp=da.UpdateCommand.Parameters.Add("@Sex",SqlDbType.VarChar);sp.SourceColumn="Sex";sp.SourceVersion=DataRowVersion.Current;sp=da.UpdateCommand.Parameters.Add("@Age",SqlDbType.VarChar);sp.SourceColumn="Age";sp.SourceVersion=DataRowVersion.Current;try{da.Update(ds,"axl1");MessageBox.Show("已经更新");}catch(Exceptionex){MessageBox.Show(ex.ToString());}finally{con.Close();}if(ds.Tables[0].Rows.Count==0){bntdelete.Enabled=false;btnupdate.Enabled=false;}}privatevoidbntdelete_Click(objectsender,System.EventArgse){stringdelete="deletefromaxl1whereFlightCode=@FlightCode";da.DeleteCommand=newSqlCommand(delete,con);sp=da.DeleteCommand.Parameters.Add("@FlightCode",SqlDbType.VarChar);sp.SourceColumn="FlightCode";sp.SourceVersion=DataRowVersion.Current;sp=da.DeleteCommand.Parameters.Add("@Password",SqlDbType.VarChar);sp.SourceColumn="Password";sp.SourceVersion=DataRowVersion.Current;sp=da.DeleteCommand.Parameters.Add("@Name",SqlDbType.VarChar);sp.SourceColumn="Name";sp.SourceVersion=DataRowVersion.Current;sp=da.DeleteCommand.Parameters.Add("@SeatNo",SqlDbType.Int);sp.SourceColumn="SeatNo";sp.SourceVersion=DataRowVersion.Current;sp=da.DeleteCommand.Parameters.Add("@Sex",SqlDbType.VarChar);sp.SourceColumn="Sex";sp.SourceVersion=DataRowVersion.Current;sp=da.DeleteCommand.Parameters.Add("@Age",SqlDbType.VarChar);sp.SourceColumn="Age";sp.SourceVersion=DataRowVersion.Current;try{ds.Tables[0].Rows[dataGrid1.CurrentRowIndex].Delete();da.Update(ds,"axl1");MessageBox.Show("已经删除");}catch(Exceptionex){MessageBox.Show(ex.ToString());}finally{con.Close();}if(ds.Tables["axl1"].Rows.Count==0){bntdelete.Enabled=false;btnupdate.Enabled=false;}}}}
解决方案:
在DataGrid上实现对数据库的操作
解决方案:
一个界面,里面用datagrid,实施增 删 改?
解决方案:
楼主应该养成好习惯...查MSDN吧....