C#出来MYSQL的方法和处理MSSQL方法过程类似,只不过相应命令的前缀SQL换做了MySql

下面的代码写下来做备忘

 

C# 操作MySql数据库
 public static void InsertIntoDataBase(MySqlConnection conn,Theme theme)
        {  
            
string sqlcommand = "insert into processedpapers(dc_ID,dc_title,dc_titleTokens,dc_descriptionTokens,dc_len,dc_description)values(@mydcid,@dmydctitle,@mydctitletokens,@mydcdescriptiontokens,@mydclen,@mydcdescription)";
            MySqlParameter dc_ID 
= new MySqlParameter("@mydcid", MySqlDbType.Int32);
            dc_ID.Value 
= theme.dc_ID;
            MySqlParameter dc_len 
= new MySqlParameter("@mydclen", MySqlDbType.Int16);
            dc_len.Value 
= theme.dc_len;
            MySqlParameter dc_title 
= new MySqlParameter("@mydctitle", MySqlDbType.VarChar, 500);
            dc_title.Value
=theme.dc_title;
            MySqlParameter dc_titleTokens 
= new MySqlParameter("@mydctitletokens", MySqlDbType.VarChar, 500);
            dc_titleTokens.Value
=theme.dc_titleTokens;
            MySqlParameter dc_descriptionTokens 
= new MySqlParameter("@mydcdescriptiontokens", MySqlDbType.Text);
            dc_descriptionTokens 
= theme.dc_descriptionTokens;
            MySqlParameter dc_description 
= new MySqlParameter("@mydcdescription",MySqlDbType.Text);
            dc_description.Value 
= theme.dc_description;
            MySqlCommand cmd 
= new MySqlCommand(sqlcommand, conn);
            cmd.Parameters.Add(dc_ID);
            cmd.Parameters.Add(dc_len);
            cmd.Parameters.Add(dc_title);
            cmd.Parameters.Add(dc_titleTokens);
            cmd.Parameters.Add(dc_descriptionTokens);
            cmd.Parameters.Add(dc_description);
            conn.Open();
            
try
            {
                cmd.ExecuteNonQuery();
            }
            
catch (System.Exception e)
            {
                StreamWriter sw 
= new StreamWriter("C:\\myerror.txt"true, Encoding.Default);
                sw.Write(e.Message);
                sw.Close();
                
//一旦发生错误程序就停止运行,等待用户发现
                Console.Read();
            }
            conn.Close();

                
        }

        

    }
    
/// <summary>
    
/// 文章类辅助插入数据库操作
    
/// </summary>
    public class Theme
    {
        
public int dc_ID;
        
public int dc_len;
        
public string dc_title;
        
public string dc_titleTokens;
        
public string dc_descriptionTokens;
        
public string dc_description;
        
public Theme()
        {
            dc_ID 
= 0;
            dc_len 
= 0;
            dc_title 
= string.Empty;
            dc_titleTokens 
= string.Empty;
            dc_descriptionTokens 
= string.Empty;
            dc_description 
= string.Empty;
        }
    }

 

 

posted on 2010-04-07 19:24  finallyly  阅读(17528)  评论(1编辑  收藏  举报